Skip to main content

Control flow

A program runs from top to bottom, one statement after another. Control flow changes that: it chooses which statements run, and how many times.

  • If: run something only when a condition holds, or choose between values.
  • While: repeat something for as long as a condition holds.
  • For: repeat something a number of times, over a range, or over an array.
  • Match: choose one branch out of many, by an integer or an enum's case.

A condition is always a boolean. Nylon doesn't treat a number or a value as true or false by itself, so write the comparison:

let count = 3
if (count != 0) {
println("not zero")
}
Doesn't compile

A number on its own isn't a condition:

let count = 3
if (count) {
println("not zero")
}