Jumping Out of Loops with break, continue, and Labels in Dart
Use break and continue to control Dart loops, then level up with labeled statements to exit or skip specific outer loops in nested for, while, and do-while.
Published on • December 3, 2026
AI Assistant

Sometimes a loop should stop early, and sometimes an inner iteration should be skipped — and when loops nest, you need a precise way to say which loop you mean. Dart gives you break, continue, and labeled statements for exactly that.
break and continue basics
break terminates the innermost enclosing loop immediately. The classic use is an infinite loop with an escape condition:
while (true) {
if (shutDownRequested()) break;
processIncomingRequests();
}
continue does the opposite: it abandons the rest of the current iteration and jumps to the loop’s next one. In a for loop the increment runs, the condition is re-evaluated, and the body starts over:
for (int i = 0; i < candidates.length; i++) {
var candidate = candidates[i];
if (candidate.yearsExperience < 5) {
continue;
}
candidate.interview();
}
With an Iterable you’d often express the same idea with where() instead — but when the skip logic is complex, continue keeps it explicit. As covered in dart-for-in-collection-loops.md, for-in and continue also work together naturally, and closures created per iteration keep their own bindings.
The innermost-loop limitation
Here’s the crucial rule: a bare break or continue targets the loop immediately surrounding it. Nested one level deep, you cannot reach the outer loop — which is exactly the gap labels were designed to fill.
Labeled statements: the syntax
A label is an identifier followed by a colon, placed before a statement, creating a labeled statement:
outerLoop:
for (var i = 1; i <= 3; i++) {
// ...
}
You then reference it with break outerLoop; or continue outerLoop;. The Dart docs define two behaviors: break labelName; terminates the labeled statement entirely, which is useful for breaking out of a specific outer loop from within a nested one, and continue labelName; skips the rest of the current iteration of the labeled loop and proceeds with its next iteration. The innermost loop is no longer the only target — the label names your destination.
break label in nested for loops
Here is the documentation’s for matrix example. When i == 2 && j == 2, break outerLoop stops both loops:
outerLoop:
for (var i = 1; i <= 3; i++) {
for (var j = 1; j <= 3; j++) {
print('i = $i, j = $j');
if (i == 2 && j == 2) {
break outerLoop;
}
}
}
print('outerLoop exited');
Output:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
outerLoop exited
continue label in nested for loops
Change the same structure to continue outerLoop and the inner loop jumps straight to the next outer iteration, skipping the rest of i = 2:
outerLoop:
for (var i = 1; i <= 3; i++) {
for (var j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
continue outerLoop;
}
print('i = $i, j = $j');
}
}
The printed matrix runs i = 1 fully, then prints only i = 2, j = 1 before jumping to i = 3 — the labeled continue skipped j = 2 and j = 3 of that outer pass and moved on. Compare that with the break version above, which stopped everything at the very same condition.
Labels work with while and do-while too
The docs show identical i/j matrices for the other loop kinds. For a while loop with break, remember that you own the increment, so the inner loop must advance j itself:
var i = 1;
outerLoop:
while (i <= 3) {
var j = 1;
while (j <= 3) {
print('i = $i, j = $j');
if (i == 2 && j == 2) {
break outerLoop;
}
j++;
}
i++;
}
print('outerLoop exited');
The do-while versions behave the same way, with the condition checked at the bottom of each iteration. One caution specific to continue in while and do-while: those loops have no automatic increment, so continue outerLoop must advance the outer counter itself — the docs’ examples do i++ before continuing — or you will spin forever on the same row.
Labels with switch cases
Labels aren’t only for loops: you can label a switch statement and terminate it with break weekend; from inside a case. More importantly, continue label inside a switch case can continue a labeled loop that wraps the switch, letting a single case decide which outer iteration to skip. That is Dart’s explicit, readable replacement for silent case fallthrough — the target of the jump is always written down.
A quick reference table
| Statement | Targets | Effect |
|---|---|---|
break; | Innermost loop or switch | Stops it immediately |
continue; | Innermost loop | Skips to its next iteration |
break label; | The labeled statement | Ends that whole loop, however deep |
continue label; | The labeled loop | Skips rest of this outer iteration |
Refactors that often avoid labels
Labels are a sharp tool, not the first one. Extract the nested loops into a function and use a plain return to exit both levels — often the clearest escape hatch of all. Alternatively, a var found = false; flag checked by the inner loop lets you break normally and test the flag afterwards.
Labels become the clearest option when the skip condition genuinely belongs to the outer iteration — search grids, matrix scans, and parser loops — and extracting the loops would only obscure the algorithm. In those cases a well-named label (searchRow:, outerLoop:) documents intent better than a flag ever could, and it costs no extra state to maintain.
Takeaways
break and continue handle the common single-loop cases, and they always act on the innermost loop. When you need to escape or skip an outer iteration — in for, while, do-while, or a switch — a labeled statement names the target precisely. Reach for extraction or a flag first, but don’t hesitate to label a loop when it makes the control flow self-explanatory.
Takeaway: Use break and continue for the innermost loop, and attach a label to the outer loop whenever you need to exit or skip it from inside nested iterations.