Java break, continue, Labeled Statements

Java break, continue & Labeled Statements

Control flow statements such as break, continue, and labeled statements provide fine-grained control over loop execution. They allow a program to interrupt, skip, or redirect execution paths inside iterative constructs. These statements are especially important in nested loops, complex iteration logic, and performance-sensitive code.

A precise understanding of these statements is essential for writing correct, readable, and efficient Java programs.


Purpose of Loop Control Statements

Loop control statements are used to:

Without these statements, loops would always run until their condition becomes false, which is often inefficient or incorrect.


break Statement

Definition

The break statement terminates the nearest enclosing loop or switch statement immediately. Control moves to the statement following the loop or switch.


Syntax

break;

Example: break in a loop

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}

Output

1
2
3
4

Once i becomes 5, the loop stops completely.


Execution Characteristics


Use Cases


break in Nested Loops

By default, break exits only the innermost loop.

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            break;
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}

Here:


continue Statement

Definition

The continue statement skips the current iteration of a loop and moves directly to the next iteration.


Syntax

continue;

Example: continue in a loop

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
}

Output

1
2
4
5

The iteration where i == 3 is skipped.


Execution Characteristics


Use Cases


continue in Nested Loops

Like break, continue affects only the innermost loop by default.

for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            continue;
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}

Here:


Labeled Statements

Definition

A labeled statement assigns a name (label) to a loop. This label can then be used with break or continue to control outer loops explicitly.


Syntax

labelName:
loop_statement

Example:

outerLoop:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            break outerLoop;
        }
        System.out.println(i + " " + j);
    }
}

Labeled break

Behavior

A labeled break terminates the loop associated with the label, not just the nearest loop.


Example

outer:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
            break outer;
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}

Execution Result


Use Cases


Labeled continue

Behavior

A labeled continue skips the current iteration of the labeled loop, not just the innermost one.


Example

outer:
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            continue outer;
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}

Here:


Comparison: break vs continue

Aspectbreakcontinue
EffectTerminates loopSkips iteration
Loop EndsYesNo
Execution Moves ToAfter loopNext iteration
Common UseExit earlySkip conditions

Comparison: Unlabeled vs Labeled

FeatureUnlabeledLabeled
ScopeInnermost loopSpecific outer loop
ReadabilitySimpleRequires discipline
UsageCommonAdvanced scenarios

Guidelines for Using Labeled Statements

Labeled statements are powerful but should be used carefully and intentionally.


Common Mistakes

These mistakes often lead to confusing execution paths.


Role in Large Codebases

In large systems:

Correct use of break, continue, and labels contributes to efficient and maintainable code.


Conclusion

The break, continue, and labeled statements provide precise control over loop execution in Java. break terminates loops, continue skips iterations, and labeled statements extend this control to outer loops in nested structures. When used appropriately, these statements improve performance, clarity, and correctness. Mastery of loop control constructs is essential for writing robust and maintainable Java programs.

🤖
PrepCampusPlus AI Tutor
Scroll to Top