Lesson · Control Flow
Loop control: break, continue, and labelled exits
9 min readLoop Control
break ends the nearest loop, continue skips to its next round, and a label lets break name exactly which loop it means in nested structures. Used well, they flatten code instead of knotting it.
break and continue, precisely
break exits the nearest enclosing loop (or switch) immediately; nothing after it in the body runs, and the loop's remaining rounds never happen. continue ends the current round only: the update and the test run again, and the loop carries on. Both act on the innermost enclosing construct, which is the fine print that trips people in nested loops.
As signals, they read well when used early. continue at the top of a body to skip invalid items is a guard clause inside a loop, and it flattens what would otherwise become an if-block wrapped around everything. break after finding the answer beats a boolean flag threaded through the loop condition.
Labelled break: naming the loop you mean
In nested loops, break leaves only the inner one. When the search is genuinely over, the outer loop must exit too, and the traditional workarounds are a flag checked at every level or a premature return. Java offers a third: a label. outer: before a loop, then break outer; leaves both in one statement.
Labels also work with continue: continue outer; skips to the next round of the named loop, which is the shape for grid scans that move to the next row. Use them sparingly and name them for intent (rows, search), because a label makes the jump visible from every line inside.
return, flags, and choosing
If the loop lives in its own method, return is usually the cleanest exit: it carries the result out and ends the search in one statement. The flag pattern (boolean found = false; plus a condition on it) is what return and labelled break were invented to replace; keep it for cases where the loops must finish collecting rather than stop early.
One rule survives all style debates: a jump statement should move control somewhere obvious. break after a match, continue on a guard, return from a search method are all visible at a glance. A break buried in a deep if inside a while inside a for is where loops start needing comments.
Why there is no goto
Java reserves goto as a keyword and never implemented it. Arbitrary jumps make control flow unreadable and optimisation hard, and the language's designers judged labels plus break and continue sufficient for the legitimate uses. The reserved word is a fossil worth knowing for quizzes, nothing more.
break leaves the nearest loop, not the one you are thinking of
In a nested loop, an unlabelled break exits the inner loop and the outer one happily continues. The result compiles, runs, and produces extra iterations that look impossible until you count which loop was left. When nested loops must exit together, write the label and mean it.
Interview angles
Difference between break and continue?+
break leaves the nearest enclosing loop entirely; continue ends the current round and the loop proceeds with its next update and test. In nested loops both act on the innermost enclosing construct unless a label names another.
How do you exit two nested loops at once?+
A labelled break on the outer loop (outer: for ... break outer;), or extract the loops into a method and return. The flag-checked-at-every-level pattern compiles but exists only because labels were forgotten.
Does Java have goto?+
No. goto is a reserved keyword that was never implemented. Labelled break and continue cover the structured uses, and arbitrary jumps were judged more harmful than useful.