Java Break and Continue — Loop Control Statements

2 minute read

Sometimes you need to exit a loop early or skip certain iterations. Java provides break and continue for this.


break — Exit the Loop

break immediately terminates the innermost loop.

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break;  // exit the loop
    }
    System.out.print(i + " ");
}
// Output: 1 2 3 4
int[] numbers = {3, 7, 2, 9, 5, 1};
int target = 9;

for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] == target) {
        System.out.println("Found at index " + i);
        break;  // no need to keep searching
    }
}

continue — Skip to Next Iteration

continue skips the rest of the current iteration and jumps to the next one.

for (int i = 1; i <= 10; i++) {
    if (i % 3 == 0) {
        continue;  // skip multiples of 3
    }
    System.out.print(i + " ");
}
// Output: 1 2 4 5 7 8 10

Common Use: Filter

String[] names = {"Alice", "", "Bob", null, "Charlie"};

for (String name : names) {
    if (name == null || name.isEmpty()) {
        continue;  // skip invalid entries
    }
    System.out.println("Hello, " + name);
}

Break and Continue in While Loops

int i = 0;
while (true) {
    i++;
    if (i % 2 == 0) continue;  // skip even numbers
    if (i > 10) break;          // stop after 10
    System.out.print(i + " ");
}
// Output: 1 3 5 7 9

Labeled Break — Exiting Nested Loops

break only exits the innermost loop. To exit an outer loop, use a label:

outer:
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
        if (i * j > 6) {
            System.out.println("Breaking at i=" + i + " j=" + j);
            break outer;  // exits BOTH loops
        }
    }
}
// Output: Breaking at i=2 j=4

Labeled Continue

outer:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (j == 1) {
            continue outer;  // skip to next i
        }
        System.out.println("i=" + i + " j=" + j);
    }
}

Output:

i=0 j=0
i=1 j=0
i=2 j=0

When to Use Break vs Continue

Use break Use continue
Found what you’re looking for Skip invalid/unwanted items
Error condition — stop looping Filter within a loop
Exit infinite loop Skip special cases

Best Practices

  1. Don’t overuse — if every iteration has break/continue, consider restructuring.
  2. Labeled breaks are rare — if you need them often, extract the inner loop into a method.
  3. Guard clauses with continue can simplify loop bodies:
// Without continue — deeply nested
for (Order order : orders) {
    if (order != null) {
        if (order.isValid()) {
            process(order);
        }
    }
}

// With continue — flat and clean
for (Order order : orders) {
    if (order == null) continue;
    if (!order.isValid()) continue;
    process(order);
}

Quick Exercises

  1. Print numbers 1 to 20, but skip all multiples of 4.
  2. Find the first negative number in an array and print its index.
  3. Use a labeled break to exit a 3×3 nested loop when the sum of indices exceeds 3.

Summary

Statement Effect
break Exits the innermost loop
continue Skips to the next iteration
break label Exits the labeled outer loop
continue label Skips to next iteration of outer loop

What’s Next?

You’ve mastered control flow. Now let’s learn how to organize code into reusable blocks — methods (functions): Functions in Java.

Updated: