Similarly to the majority of modern programming languages, the Scala language has a bunch of control structures; for example, for branching and looping. The control structures in question are if, while, for, and pattern matching.
Control structures
If and While
if and while are implemented the same way as they are in any other programming language:
scala> val flag = true
flag: Boolean = true
scala> if (flag) {
| println("Flag is true")
| }
Flag is true
scala> if (!flag) {
| println("Flag is false")
| } else {
| println("Flag is true")
| }
Flag is true
scala> var x: Int = 0
x: Int = 0
scala> while (x < 5) {
| x += 1
| println(s"x = $x")
|...