break / continue

break and continue alter the flow of a loop:

  • break exits the innermost loop immediately.
  • continue skips the rest of the current iteration and moves on — re-evaluating the condition in a while, or advancing to the next element in a for.
while true {
    let input = ReadLine();
    if input == "quit" { break; }     // leave the loop
    if input == "" { continue; }      // skip blank lines
    Process(input);
}

Targeting an Outer Loop

By default both act on the innermost enclosing loop. To affect an outer loop instead, give that loop a label and name it: break label or continue label (see Labels):

outer: for row in rows {
    for cell in row {
        if cell == target { break outer; }   // exit both loops
    }
}

See Also

  • loop — infinite loops and how labels are declared
  • while — looping on a condition
  • for / in — iterating a collection or range