while Loops and continue Expressions
Loop with conditions and step actions.
while Loops and continue Expressions is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Avoiding Infinite Loops
Each iteration should move toward making the condition false. Forget to update the variable and the loop spins forever.
The continue Expression
Zig adds a continue expression after a colon. It runs at the end of every iteration, the perfect place for your step update.
var i: usize = 0;
while (i < 5) : (i += 1) {
work(i);
}Why the continue Slot Helps
Putting the step in the continue slot keeps it visible at the top and guarantees it runs even after a continue statement skips ahead.
Skip with continue
The continue statement jumps to the next iteration immediately, skipping the rest of the body but still running the continue expression.
while (i < 10) : (i += 1) {
if (i % 2 == 0) continue;
odd_sum += i;
}Stop Early with break
Use break to leave a while loop right away, even when the condition is still true. Control resumes after the loop.
while (true) {
if (done()) break;
}while as an Expression
A while can yield a value. break supplies the result on success, and an else clause provides the value when the loop finishes normally.
const found = while (i < n) : (i += 1) {
if (match(i)) break i;
} else null;Looping over Optionals
A while can keep unwrapping an optional, running while values are present and ending the moment it sees null.
while (it.next()) |item| {
process(item);
}Looping over Errors
Like if, a while can capture an error in an else branch when the unwrapped expression returns an error union instead of a value.
Infinite Loops on Purpose
Sometimes you want a deliberate forever loop. Writing while (true) with a break inside is the idiomatic event-loop pattern in Zig.
Quick Check
Where does Zig's continue expression run during a while loop?
Recap
You drove while: condition-first looping, the continue expression for clean steps, break and continue, and the value-returning expression form. 🎯
Frequently asked questions
Is the “while Loops and continue Expressions” lesson free?
Yes — the full text of “while Loops and continue Expressions” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “while Loops and continue Expressions”?
Loop with conditions and step actions. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “while Loops and continue Expressions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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