Learn Java
import com.learnjava.controlflow.BreakContinueLabels;

break, continue & Labeled Loops in Java

@Concept

break exits a loop (or a switch) immediately, skipping any remaining iterations entirely; continue skips just the rest of the current iteration's body and jumps straight to the next one, without leaving the loop altogether. Both work automatically on the innermost loop they're written inside β€” which is exactly where labeled loops come in: prefixing a loop with a label, like outer:, and then writing break outer; or continue outer; from inside a nested loop lets that statement target the labeled outer loop specifically, instead of only the nearest one.

Labeled break and continue exist specifically for nested loops, since an unlabeled break or continue inside a nested loop can only ever affect the loop it's directly inside. Reaching an outer loop from deep inside a nested one otherwise usually requires an extra boolean flag checked at each level, which labels avoid entirely.

@HowItWorks
1

break

Exits the loop it's directly inside immediately, skipping every remaining iteration.

2

continue

Skips the rest of the current iteration's body only, then moves on to the next iteration as normal.

3

Both default to the innermost loop

An unlabeled break or continue only ever affects the loop it's written directly inside, never an outer one.

4

Labeling a loop

Writing a name followed by a colon, like outer:, right before a loop lets that specific loop be targeted from anywhere inside it.

5

Labeled break and continue

break outer; or continue outer; from inside a nested loop acts on the labeled outer loop specifically, not the nearest enclosing one.

@Example
public class BreakContinueDemo {
public static void main(String[] args) {
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue outer;
}
System.out.println(i + "," + j);
}
}
}
}
Output
1,1
2,1
3,1
1When i is 1 and j is 1, the if is false, so "1,1" prints normally.
2When j becomes 2, the if is true, and continue outer skips the rest of the inner loop entirely, jumping straight to the next value of i instead of just the next value of j.
3That pattern repeats identically for i equal to 2 and 3, since j reaches 2 each time before it could reach 3.
4Exactly three lines print in total, one per value of i, because the inner loop never gets past j equal to 1 in any of them.
@UseCases
Exiting a search loop immediately once a match is found, with break
Skipping invalid or unwanted items in a loop without exiting it entirely, with continue
Escaping multiple levels of nested loops at once from deep inside, using a labeled break
Replacing an error-prone boolean flag that used to be checked at every level of nested loops

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself β€” building beats watching.