Control Structures (if, match, loops)

Scala supports standard control structures like if, for, while, and powerful pattern matching with match.

Examples

val num = 5
if (num > 0) println("Positive") else println("Negative")
val result = num match {
  case 0 => "Zero"
  case x if x > 0 => "Positive"
  case _ => "Negative"
}
for (i <- 1 to 3) println(i)

Pattern matching is a powerful alternative to switch-case, and can deconstruct values too.

← PrevNext →