DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Part 2: Labeled `break`, Labeled `continue`, and `continue` in `do-while`

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 continue behaves differently inside a do-while loop?

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 (...) {

}
Enter fullscreen mode Exit fullscreen mode

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;

}
Enter fullscreen mode Exit fullscreen mode

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);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

(no output)
Enter fullscreen mode Exit fullscreen mode

Why?

The very first iteration is:

row = 1
column = 1
Enter fullscreen mode Exit fullscreen mode

Since

row == column
Enter fullscreen mode Exit fullscreen mode

is true,

break outerLoop;
Enter fullscreen mode Exit fullscreen mode

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);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

2 - 1
3 - 1
3 - 2
Enter fullscreen mode Exit fullscreen mode

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;

}
Enter fullscreen mode Exit fullscreen mode

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);

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

1 - 0
2 - 0
2 - 1
Enter fullscreen mode Exit fullscreen mode

Step-by-step

First iteration

row = 0
column = 0
Enter fullscreen mode Exit fullscreen mode

Condition becomes true.

Java immediately starts the next iteration of the outer loop.

Nothing is printed.


Second iteration

row = 1
column = 0
Enter fullscreen mode Exit fullscreen mode

Printed.

Next,

column = 1
Enter fullscreen mode Exit fullscreen mode

Condition becomes true.

Jump to the next outer iteration.


Third iteration

2 - 0
2 - 1
Enter fullscreen mode Exit fullscreen mode

Then

column = 2
Enter fullscreen mode Exit fullscreen mode

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);

    }

}
Enter fullscreen mode Exit fullscreen mode

Let's replace the highlighted statement.


Case 1 — break

break;
Enter fullscreen mode Exit fullscreen mode

Output

1 - 0
2 - 0
2 - 1
Enter fullscreen mode Exit fullscreen mode

Only the inner loop exits.


Case 2 — break outerLoop

break outerLoop;
Enter fullscreen mode Exit fullscreen mode

Output

(no output)
Enter fullscreen mode Exit fullscreen mode

Both loops terminate immediately.


Case 3 — continue

continue;
Enter fullscreen mode Exit fullscreen mode

Output

0 - 1
0 - 2
1 - 0
1 - 2
2 - 0
2 - 1
Enter fullscreen mode Exit fullscreen mode

Only the current inner-loop iteration is skipped.


Case 4 — continue outerLoop

continue outerLoop;
Enter fullscreen mode Exit fullscreen mode

Output

1 - 0
2 - 0
2 - 1
Enter fullscreen mode Exit fullscreen mode

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");
Enter fullscreen mode Exit fullscreen mode

Output

Validation started
Validation completed
Enter fullscreen mode Exit fullscreen mode

The labeled block exits immediately.


continue Cannot Be Used with Ordinary Blocks

This is illegal.

validation:

{

    continue validation;

}
Enter fullscreen mode Exit fullscreen mode

Compile-time error

continue outside of loop
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Output

1
2
4
5
Enter fullscreen mode Exit fullscreen mode

Why?

Unlike while, the condition in a do-while loop is checked after the loop body.

When Java encounters

continue;
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

This creates an infinite loop.

Why?

When

number = 3
Enter fullscreen mode Exit fullscreen mode

execution reaches

continue;
Enter fullscreen mode Exit fullscreen mode

The increment statement

number++;
Enter fullscreen mode Exit fullscreen mode

is skipped.

Therefore,

number
Enter fullscreen mode Exit fullscreen mode

always remains

3
Enter fullscreen mode Exit fullscreen mode

The condition

number <= 5
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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:

  • Bbreak exits the nearest loop.
  • Ccontinue skips one iteration.
  • Ddo-while + continue requires 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 break exits only the nearest enclosing loop.
  • A labeled break can exit any labeled loop or block.
  • A normal continue skips only the current iteration.
  • A labeled continue skips the current iteration of the labeled outer loop.
  • continue cannot be used with ordinary blocks.
  • do-while with continue is 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)