© 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 ...

Get Scala for Java Developers: A Practical Primer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.