Break and Continue

Break and Continue in Java — Complete Guide

Break and Continue

Java gives you three ways to jump around inside a loop's normal top-to-bottom flow: break, continue, and labels. This post covers what each one does, where you're allowed to use them, and how labeled versions change behavior in nested loops — with the exact output for every example.

Transfer Statements

  1. break;
  2. continue;
  3. label

Break

The break statement is used to terminate a loop, switch, or labeled block immediately. When it's encountered inside a loop, the iteration stops right there and control jumps straight to the first statement after the loop. Break statements are typically used when you're not sure of the exact number of iterations needed, or you want to stop based on some condition.

You can use break in exactly three places:

1. Inside a switch, to stop fallthrough

int x = 0;
switch (x) {
    case 0:
        System.out.print("hello");
    case 1:
        break;
    case 2:
        System.out.print("Hi");
    default:
        System.out.print("default");
}
    

Output: both case 0 and case 1 execute (case 0 has no break, so it falls through into case 1).

2. Inside a loop, to stop iteration based on a condition

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

Output: 0 1 2 3 4

3. Inside a labeled block, to break out based on a condition

class Test {
    public static void main(String... args) {
        int x = 10;
        l1: {
            System.out.print("Begin");
            if (x == 10)
                break l1;
            System.out.print("end");
        }
        System.out.print("Hello");
    }
}
    

Output: Begin Hello

These are the only three places break is allowed. Use it anywhere else, and you'll get a compile-time error: break outside switch and loop.


Continue

The continue statement skips the rest of the current loop iteration and jumps straight to the next one.

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0)
        continue;
    System.out.print(i);
}
    

Output: 1 3 5 7 9

continue is only valid inside a loop. Use it anywhere else, and you'll get a compile-time error: continue outside loop.

class Test {
    public static void main(String... args) {
        int i = 10;
        if (i == 10)
            continue;
        System.out.print("Hello");
    }
}
    

Compile-time error: continue outside loop


Labeled Break & Continue

In nested loops, a plain break or continue only affects the innermost loop. Labeled versions let you target a specific outer loop directly.

l1:
for ( ... ) {
    l2:
    for ( ... ) {
        for ( ... ) {
            break l1;
            break l2;
        }
    }
}
    
Labeled break and continue in nested loops

Case 1 — Plain break

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

Output: 1 0 / 2 0 / 2 1

A plain break only exits the inner loop, so the outer loop keeps running.

Case 2 — break l1 (labeled)

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

Output: no output

Here, i == j is true on the very first iteration (i=0, j=0), and break l1 exits the outer loop entirely before anything gets printed.

Case 3 — Plain continue

l1:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == j)
            continue;
        System.out.println(i + " " + j);
    }
}
    

Output: 0 1 / 0 2 / 1 0 / 1 2 / 2 0 / 2 1

Case 4 — continue l1 (labeled)

l1:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == j)
            continue l1;
        System.out.println(i + " " + j);
    }
}
    

Output: 1 0 / 2 0 / 2 1

continue l1 skips straight to the next iteration of the outer loop, not just the inner one — which is why fewer pairs get printed compared to Case 3.


do-while & Continue

int x = 0;
do {
    x++;
    System.out.print(x);
    if (++x < 5)
        continue;
    x++;
    System.out.print(x);
} while (++x < 10);
    

Output: 1 4 6 8 10

Conclusion

break exits a loop, switch, or labeled block entirely; continue skips just the current iteration and moves on to the next one. Both work exactly as expected in a single loop — the real thing to remember is what changes in nested loops: a plain break/continue only affects the innermost loop, while a labeled version (break l1, continue l1) lets you target an outer loop directly, which is often the difference between "no output at all" and the result you actually wanted.

1 Comments

Anonymous said…
Simple way to understand how to use countinue