Break / continue

Exit a loop early or skip to the next iteration.

break exits the innermost enclosing loop immediately. continue skips the rest of the current iteration and jumps to the next one.

Both require a semicolon: break; and continue;.

They work inside **while**, C-style **for**, **for (item von array)**, and **for (key, value von map)** loops. Using either outside a loop is a compile-time error.

In a C-style for loop with an increment clause, continue runs the increment before re-checking the condition: same as C.

break in while

Leave the loop immediately: no further iterations run.

break in while
var n = 0;
while (n < 10) {
  n = n + 1;
  if (n == 4) break;
  print n;
}
output
1
2
3

continue in for

Skip the rest of this iteration and jump to the next one.

continue in for
for (var i = 0; i < 5; i = i + 1) {
  if (i == 2) continue;
  print i;
}
output
0
1
3
4

break and continue in for-von

`break` and `continue` work in `von` loops the same as in `while` and C-style `for`.

break and continue in for-von
var xs = #[1, 2, 3, 4, 5];

for (x von xs) {
  if (x == 2) continue;
  if (x == 4) break;
  print x;
}
output
1
3

Nested loops: break inner only

`break` always targets the innermost loop: outer loops keep running.

Nested loops: break inner only
var i = 0;
while (i < 3) {
  i = i + 1;
  var j = 0;
  while (j < 3) {
    j = j + 1;
    if (j == 2) break;
    print i;
    print j;
  }
}
output
1
1
2
1
3
1

Notes