break, continue, and Labeled Loops
Control loops precisely with labels.
break, continue, and Labeled Loops is a free Zig Academy lesson on CoddyKit — lesson 4 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.
Two Loop Controls
Inside any loop, break exits the loop entirely while continue jumps to the next iteration. They give you fine control over flow. 🎛️
break Leaves Now
When break runs, the loop stops immediately and execution resumes on the line right after the loop. Nothing else in the body runs.
for (items) |x| {
if (x == 0) break;
use(x);
}continue Skips Ahead
A continue abandons the rest of the current pass and moves on to the next iteration, leaving the loop itself running.
for (items) |x| {
if (x < 0) continue;
total += x;
}They Target the Innermost Loop
By default break and continue affect only the innermost loop around them. An outer loop keeps running as if nothing happened.
Naming a Loop
You can attach a label to a loop with a name and a colon. That label gives you a handle to target it from deeper inside.
outer: for (rows) |row| {
for (row) |cell| {
check(cell);
}
}break to a Label
Write break :outer to exit a named outer loop directly from within a nested loop. One statement unwinds both levels.
outer: for (rows) |row| {
for (row) |cell| {
if (cell == target) break :outer;
}
}continue to a Label
Use continue :outer to skip to the next iteration of a labeled outer loop, abandoning the rest of the inner loop instantly.
Labels Replace Flags
Before labels you might set a found flag and check it after each loop. Labeled break removes that boilerplate and reads more clearly.
Labeled break Returns a Value
A labeled loop can also be an expression. break :outer value hands that value back as the result of the whole labeled loop.
const hit = outer: for (rows) |row| {
for (row) |c| {
if (c == target) break :outer c;
}
} else 0;Labeled Blocks Too
Labels are not just for loops. A plain block can be labeled, and break with a value turns that block into an expression.
const x = blk: {
const t = compute();
break :blk t * 2;
};Keep Nesting Shallow
Labeled control is powerful, but deeply nested loops hurt readability. Reach for labels when they clarify, not to excuse tangled code.
Quick Check
From an inner loop you want to exit the outer loop named outer. Which statement does it?
Recap
You mastered break and continue: innermost by default, labeled loops for nesting, value-returning labeled breaks, and labeled blocks. 🎯
Frequently asked questions
Is the “break, continue, and Labeled Loops” lesson free?
Yes — the full text of “break, continue, and Labeled Loops” 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 “break, continue, and Labeled Loops”?
Control loops precisely with labels. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “break, continue, and Labeled Loops” 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