🔀 Control Flow · Beginner

break & continue in Java

Exit the loop vs skip to the next iteration.

🧩 The mysteryYou're scanning 1,000,000 orders for the first fraud. You find it at order #12. Do you really have to check the other 999,988?

break: get out now

break immediately exits the innermost loop (or switch). The rest of the body and all remaining passes are skipped; execution continues after the loop.

for (int i = 1; i <= 100; i++) {
    if (i * i > 50) {
        break;
    }
    System.out.print(i + " ");
} // 1 2 3 4 5 6 7

continue: skip this one

continue skips the rest of the current pass and moves on to the next one. The loop itself keeps going. In a for loop, **the update (i++) still runs** before the next check.

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue; // skip 3
    }
    System.out.print(i);
} // 1245
🔮 Predict it

Your turn

What prints?

for (int i = 1; i <= 6; i++) {
    if (i % 3 == 0) continue;
    if (i == 5) break;
    System.out.print(i);
}
  1. 124
  2. 1245
  3. 12
  4. 12456
Show the answer

1 and 2 print. At 3, continue skips the print. 4 prints. At 5, break exits before printing. Result: 124.

⚠️ The trap

continue in a while loop

In a while loop the update is just a normal line in the body, and continue jumps over it. Below, when i hits 2, continue skips i++, so i stays 2 forever: an infinite loop. Increment before continuing, or use a for loop.

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

Only the innermost loop

break and continue affect only the innermost loop they're in. A break inside a nested loop leaves the inner loop; the outer loop carries on with its next pass.

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (j == 1) break; // inner only
        System.out.print(i + "" + j + " ");
    }
} // 00 10 20
🔮 Predict it

Nested loops

The inner loop breaks when j > i. What prints?

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (j > i) break;
        System.out.print(i + "" + j + " ");
    }
}
  1. 00
  2. 00 10 11 20 21 22
  3. 00 11 22
  4. 00 01 02
Show the answer

Each break only ends the *inner* loop. Row 0 prints 00, row 1 prints 10 11, row 2 prints 20 21 22. The outer loop never stops early.

💼 In the real world

In real projects

Early exits save real time: stop searching once you've found the user, stop retrying once the upload succeeds. continue keeps code flat: "skip invalid rows" at the top of a loop instead of nesting everything inside an if. But a misplaced break can be catastrophic (see the fun fact).

Key takeaways

  1. break: leave the loop entirely
  2. continue: skip to the next iteration
  3. In a for loop, continue still runs the update (i++)
  4. In a while loop, continue can skip your i++ and loop forever

💡 break is walking out of the cinema; continue is skipping one boring scene and staying for the next.

🤯 Did you know?

On 15 January 1990, a misplaced break in C code was blamed for crashing AT&T's long-distance network for hours, leaving millions of calls unconnected.

Practice questions

What does this print?

for (int i = 0; i < 6; i++) {
    if (i % 2 == 0) {
        continue;
    }
    System.out.print(i);
}
  1. 024
  2. 135
  3. 0
  4. 012345
Check your answer

135. For even i, continue skips the print. Only the odd values 1, 3 and 5 get printed.

What does this print?

int i = 0;
while (true) {
    i++;
    if (i == 3) continue;
    if (i > 4) break;
    System.out.print(i);
}
  1. 1234
  2. 12
  3. 124
  4. 1245
Check your answer

124. i = 1, 2 print. At 3, continue skips the print. 4 prints. At 5, i > 4 triggers break.

Next: break only escapes ONE loop. What if you're three loops deep and want out of all of them?