© Toby Weston 2018

Toby Weston, Scala for Java Developers, https://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.


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

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.