In Part 1, we learned how break and continue work in loops and switch statements.
However, when working with nested loops, a normal break or continue only affects the innermost loop.
What if you want to:
- Exit an outer loop directly?
- Skip an entire outer loop iteration?
- Understand why
continuebehaves differently inside ado-whileloop?
Java solves these problems with labeled break and labeled continue.
Let's explore them.
What Is a Label?
A label is simply an identifier followed by a colon (:).
outerLoop:
for (...) {
}
A label does nothing by itself.
It becomes useful only when used with:
break labelName;continue labelName;
Labeled break
Normally, break exits only the nearest enclosing loop.
A labeled break lets you exit any labeled loop or block.
Syntax
labelName:
for (...) {
break labelName;
}
Example: Exiting Nested Loops
outerLoop:
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (row == column) {
break outerLoop;
}
System.out.println(row + " - " + column);
}
}
Output
(no output)
Why?
The very first iteration is:
row = 1
column = 1
Since
row == column
is true,
break outerLoop;
terminates both loops immediately.
Compare with a Normal break
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (row == column) {
break;
}
System.out.println(row + " - " + column);
}
}
Output
2 - 1
3 - 1
3 - 2
Only the inner loop terminates.
The outer loop continues.
Labeled continue
A normal continue skips only the current iteration of the innermost loop.
A labeled continue skips the remaining iterations of the current outer loop iteration.
Syntax
labelName:
for (...) {
continue labelName;
}
Example
outerLoop:
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (row == column) {
continue outerLoop;
}
System.out.println(row + " - " + column);
}
}
Output
1 - 0
2 - 0
2 - 1
Step-by-step
First iteration
row = 0
column = 0
Condition becomes true.
Java immediately starts the next iteration of the outer loop.
Nothing is printed.
Second iteration
row = 1
column = 0
Printed.
Next,
column = 1
Condition becomes true.
Jump to the next outer iteration.
Third iteration
2 - 0
2 - 1
Then
column = 2
Condition becomes true.
Loop ends.
Understanding All Four Behaviors
Consider the same nested loop.
outerLoop:
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (row == column) {
// Replace this statement
}
System.out.println(row + " - " + column);
}
}
Let's replace the highlighted statement.
Case 1 — break
break;
Output
1 - 0
2 - 0
2 - 1
Only the inner loop exits.
Case 2 — break outerLoop
break outerLoop;
Output
(no output)
Both loops terminate immediately.
Case 3 — continue
continue;
Output
0 - 1
0 - 2
1 - 0
1 - 2
2 - 0
2 - 1
Only the current inner-loop iteration is skipped.
Case 4 — continue outerLoop
continue outerLoop;
Output
1 - 0
2 - 0
2 - 1
The remaining inner-loop iterations are skipped, and execution continues with the next outer-loop iteration.
Labeled break Can Exit Blocks Too
Labels aren't limited to loops.
int age = 25;
validation:
{
System.out.println("Validation started");
if (age > 18) {
break validation;
}
System.out.println("Minor");
}
System.out.println("Validation completed");
Output
Validation started
Validation completed
The labeled block exits immediately.
continue Cannot Be Used with Ordinary Blocks
This is illegal.
validation:
{
continue validation;
}
Compile-time error
continue outside of loop
continue always requires a loop.
The Tricky Part: continue in a do-while Loop
This is one of the most confusing topics for beginners.
Consider the following.
int number = 1;
do {
if (number == 3) {
number++;
continue;
}
System.out.println(number);
number++;
} while (number <= 5);
Output
1
2
4
5
Why?
Unlike while, the condition in a do-while loop is checked after the loop body.
When Java encounters
continue;
it immediately jumps to the condition check.
Then the next iteration begins.
A Dangerous Mistake
Consider this.
int number = 1;
do {
if (number == 3) {
continue;
}
System.out.println(number);
number++;
} while (number <= 5);
This creates an infinite loop.
Why?
When
number = 3
execution reaches
continue;
The increment statement
number++;
is skipped.
Therefore,
number
always remains
3
The condition
number <= 5
always remains true.
The loop never ends.
while vs do-while with continue
| Feature | while |
do-while |
|---|---|---|
| Condition checked | Before loop body | After loop body |
continue jumps to |
Condition | Condition |
| Easy to create infinite loops | Less likely | More likely |
Common Beginner Mistakes
Expecting break to Exit Every Loop
A normal break exits only the nearest enclosing loop.
Use a labeled break to exit multiple loops.
Using continue Outside a Loop
continue;
Compile-time error.
Confusing continue with break
break
→ terminates the loop.
continue
→ skips only the current iteration.
Forgetting to Update Variables Before continue
This is one of the most common causes of infinite loops.
Always ensure the loop-control variable is updated before executing continue.
Interview Questions
Can a labeled break exit multiple loops?
Yes.
It exits the labeled loop or block.
Can continue target a labeled block?
No.
Labels with continue must always refer to loops.
Why is continue inside a do-while loop considered tricky?
Because the condition is evaluated after the loop body.
If the loop variable isn't updated before continue, an infinite loop can occur.
Which statement can exit a labeled block?
break.
Which statement can target only labeled loops?
continue.
Best Practices
- Avoid labels unless they genuinely improve readability.
- Prefer extracting nested loops into separate methods instead of relying heavily on labels.
- Always update loop-control variables before executing
continue. - Keep nested loops simple.
- Write code that's easy for other developers to understand.
Quick Memory Trick 🧠
Remember BCDL:
-
B →
breakexits the nearest loop. -
C →
continueskips one iteration. -
D →
do-while+continuerequires extra care. - L → Labels let you control outer loops.
Or simply remember:
Normal transfer statements affect the nearest loop. Labels let you control outer loops.
Key Takeaways
- A normal
breakexits only the nearest enclosing loop. - A labeled
breakcan exit any labeled loop or block. - A normal
continueskips only the current iteration. - A labeled
continueskips the current iteration of the labeled outer loop. -
continuecannot be used with ordinary blocks. -
do-whilewithcontinueis a common interview topic because it can easily create infinite loops. - Use labels sparingly to keep code readable.
If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.
Happy Coding!
Top comments (0)