Java Break and Continue

Loops repeat work again and again. Sometimes you need to stop a loop early or skip one round. Java gives you two keywords for these jobs: break and continue. Break ends the loop. Continue skips the current round and moves to the next one.

A Cricket Ground Example

A runner jogs laps around a ground. The coach shouts "stop" and the runner leaves the track. That shout works like break. A different shout, "skip this lap", makes the runner cross the lap without running it and join the next lap. That shout works like continue.

The Break Statement

Break jumps out of the nearest loop or switch at once. The program continues with the first line after the loop.

for (int i = 1; i <= 10; i++) {
    if (i == 4) {
        break;
    }
    System.out.println("Round " + i);
}
System.out.println("Loop ended");

Output:

Round 1
Round 2
Round 3
Loop ended

Diagram: Break

 i=1 --> print --> i=2 --> print --> i=3 --> print --> i=4
                                                       |
                                                    break!
                                                       |
                                                       v
                                               line after loop

The Continue Statement

Continue skips the rest of the current round. The loop jumps to the next round and checks its condition again.

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

Output:

Number 1
Number 2
Number 4
Number 5

Diagram: Continue

 i=1 --> print
 i=2 --> print
 i=3 --> continue (print skipped)
 i=4 --> print
 i=5 --> print

Break and Continue Side by Side

Featurebreakcontinue
Effect on loopEnds the loopSkips one round
Next stepLine after the loopNext round
Works in switchYesNo

Break Inside a Switch

Each case in a switch needs a break. Without break, Java runs the next case. This effect has the name fall-through.

int day = 2;
switch (day) {
    case 1: System.out.println("Mon"); break;
    case 2: System.out.println("Tue"); break;
    default: System.out.println("Other");
}

Real Example: Searching a List

A search program checks each name until it finds a match. Once found, the loop has no more work to do. Break saves time by ending the search.

String[] names = {"Asha", "Ravi", "Neha", "Karan"};
for (int i = 0; i < names.length; i++) {
    if (names[i].equals("Neha")) {
        System.out.println("Found at " + i);
        break;
    }
}

Labeled Break

Break normally leaves only the nearest loop. A label lets you leave an outer loop from inside an inner loop.

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

The program prints 1,1 and 1,2 and 1,3 and 2,1, and stops at 2,2.

Common Mistakes

  • Placing continue in a while loop before the counter update, which creates an endless loop
  • Forgetting break inside switch cases
  • Expecting break to leave two loops at once without a label

Continue in a While Loop

A while loop needs extra care with continue. The counter must change before the continue line runs. A missing update keeps the same value forever and the loop never ends.

int i = 0;
while (i < 5) {
    i++;
    if (i == 2) {
        continue;
    }
    System.out.println(i);
}

The program prints 1, 3, 4, and 5. The update sits above continue, so the loop always moves forward.

Quick Summary

  • Break ends a loop or a switch immediately
  • Continue skips the current round only
  • Labels let break leave an outer loop