Loops: while and for

You will learn

The three loop forms the compiler accepts and how a range is written.

while (i < n) { i = i + 1; } repeats its block while the condition is true, and the block must move toward making it false. for i in 0..n { } walks i from 0 up to n, with n itself left out. The compiler also accepts the Zig form, for (0..4) |i| { }. A loop usually updates a var declared before it.

Open the interactive lesson →