Choices: if, else and switch

You will learn

The forms of if the compiler accepts, the if expression, and switch with else.

if (a > b) { ... } else { ... } runs one of two blocks. The parentheses are optional: if a > b { ... } also compiles. if can be an expression too: return if (a > b) a else b; gives the larger one. switch (m) { 0 => 10, else => 20 } picks a value by matching m, and else covers every other case.

Open the interactive lesson →