O'Reilly logo

Scala for Java Developers: A Practical Primer by Toby Weston

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Toby Weston 2018
Toby WestonScala for Java Developershttps://doi.org/10.1007/978-1-4842-3108-1_12

12. Control Structures

Toby Weston
(1)
London, UK
 
This chapter is all about control structures, like if statements, switch blocks, loops, and breaks. Specifically, we’ll look at the following:
  • Conditionals like if statements, ternary expressions, and switches.
  • Looping structures: do, while and for
  • Breaking control flow.
  • Exceptions, briefly.

Conditionals

Ifs and Ternaries

Conditionals are straightforward.
  // java
  if (age > 55) {
      retire();
  } else {
      carryOnWorking();
  }
An if in Java looks exactly the same in Scala.
  // scala
  if (age > 55) {
    retire()
  } else {
    carryOnWorking()
  }
You’ll often find Scala developers dropping the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required