while Loops and continue Expressions
Loop with conditions and step actions.
Repeat with while
A while loop repeats its body as long as a bool condition stays true. It checks the condition before each pass. 🔁
while (i < 5) {
std.debug.print("{}\n", .{i});
i += 1;
}Checked Before Each Pass
Zig tests the condition first, so a while whose condition starts false runs zero times. The body is never guaranteed to execute.
All lessons in this course
- if as a Statement and Expression
- while Loops and continue Expressions
- for Loops over Ranges and Items
- break, continue, and Labeled Loops