while

while repeats its body as long as the condition holds. The condition is tested before each iteration, so the body may run zero times.

var i = 0;
while i < 10 {
    Print(i);
    i += 1;
}

Like every condition, a while condition must be a bool and is written without parentheses.

Exiting Early

Use break to leave the loop before the condition becomes false, and continue to skip to the next test:

while true {
    let line = ReadLine();
    if line == "quit" { break; }
    if line == "" { continue; }
    Process(line);
}

When a loop is meant to run forever until a break, prefer loop over while true — it states the intent directly.

See Also

  • for / in — when iterating a collection or a counted range
  • loop — the clearer form of an unconditional loop
  • break / continue — altering loop control flow