break, continue & Labeled Loops
break, continue & Labeled Loops
So far you have written loops that run from start to finish on their own schedule. But real programs often need to cut a loop short or skip a single turn. Java gives you two keywords for that — break and continue — plus a labeling mechanism that extends both keywords to work across nested loops.
break — leaving a loop early
When Java executes break, it immediately exits the innermost enclosing loop (or switch) and continues with the first statement after it. This is useful when you have found what you were looking for and further iterations would be wasted work.
Without break the loop would keep running through 9 and 2 even though the answer was already known at 4.
for above were nested inside another loop, break would leave the inner loop but the outer loop would keep going. More on that below.
continue — skipping one iteration
continue does not exit the loop — it skips the rest of the current iteration's body and jumps straight to the loop's update step (in a for) or back to the condition check (in a while). Use it when a particular value is uninteresting and you want to move on.
if (i % 2 != 0) { System.out.print(i + " "); } without continue. Use continue when it makes the logic noticeably clearer — for example, when skipping several lines of work.
break vs. continue at a glance
break— exit the loop entirely, run whatever comes after the loop.continue— skip the rest of this iteration, then check the loop condition again.
Labeled break — escaping nested loops
Nested loops are common in matrix traversal, grid searches, and pattern printing. When you find your target inside the inner loop, a plain break only exits the inner loop — the outer loop keeps running. A labeled break lets you exit both loops at once.
A label is just an identifier followed by a colon, placed immediately before the loop you want to be able to exit:
After break outer Java jumps to the first statement after the outer for loop — row 2 is never visited.
Labeled continue — restarting the outer loop
You can also use a label with continue. Instead of jumping to the next iteration of the inner loop, Java jumps to the next iteration of the labeled (outer) loop.
Where to put labels
Labels must appear directly before a loop statement (for, while, do-while). They cannot label arbitrary blocks. By convention, label names are written in lowercase (outer, rows) to distinguish them from constants.
return — that is usually cleaner.
A complete worked example — prime sieve sketch
Here is a snippet that uses both break and continue together: it checks each candidate number and skips composites using continue, stopping at the first prime above a threshold.
Summary
breakexits the innermost loop immediately.continueskips the rest of the current iteration and moves on.- A labeled break exits a specific outer loop by name.
- A labeled continue restarts a specific outer loop's next iteration.
- Labels are a power tool — use them when they genuinely simplify logic, and consider a helper method instead for very complex cases.