🌱BeginnerLoops

break and continue

Control loop execution precisely with break (exit the loop) and continue (skip to the next iteration).

Overview

break and continue give you fine-grained control over loop execution. break immediately exits the current loop entirely - execution jumps to the first statement after the loop. continue skips the rest of the current iteration and jumps to the next one (next condition check). Both work inside for, while, and do-while loops. They make it easy to stop searching once you find something, or to skip elements that don't need processing.

Key Points

  • •break exits the enclosing loop immediately - execution continues after the loop
  • •continue skips the rest of the current iteration - the loop's condition check runs next
  • •In a for loop, continue still runs the update expression (i++) before checking the condition
  • •In nested loops, break and continue only affect the innermost loop
  • •break also exits a switch statement - see J018
  • •Overusing break and continue can make loop logic hard to follow - use with care

Examples

break - exit the loop early

public class Main {
    public static void main(String[] args) {
        // Find the first number divisible by 7 between 1-50
        for (int i = 1; i <= 50; i++) {
            if (i % 7 == 0) {
                System.out.println("First divisible by 7: " + i); // 7
                break; // stop - we found it
            }
        }

        // Search for a name
        String[] names = {"Alice", "Bob", "Charlie", "Dave"};
        String target = "Charlie";
        for (String name : names) {
            if (name.equals(target)) {
                System.out.println("Found: " + name);
                break;
            }
        }
    }
}

break is the standard way to exit a loop once you've found what you were looking for.

continue - skip unwanted iterations

public class Main {
    public static void main(String[] args) {
        // Print all numbers 1-10 except multiples of 3
        for (int i = 1; i <= 10; i++) {
            if (i % 3 == 0) {
                continue; // skip 3, 6, 9
            }
            System.out.print(i + " "); // 1 2 4 5 7 8 10
        }
        System.out.println();

        // Skip negative numbers when summing
        int[] data = {5, -3, 8, -1, 4, -7, 2};
        int sum = 0;
        for (int n : data) {
            if (n < 0) continue; // skip negatives
            sum += n;
        }
        System.out.println("Sum of positives: " + sum); // 19
    }
}

continue is clean for filtering: "skip this element if condition X".

break in nested loops - only exits the inner loop

public class Main {
    public static void main(String[] args) {
        outer: // label for the outer loop
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) break; // only breaks inner loop
                System.out.print(i + "," + j + " ");
            }
        }
        System.out.println();
        // Output: 1,1 2,1 3,1

        // To break out of the outer loop, use a label
        search:
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == 1 && j == 1) break search;
                System.out.print(i + "" + j + " ");
            }
        }
        // Output: 00 01 02 10
    }
}

Labeled break (break outerLabel;) exits the named loop. Labels are rarely needed but exist for this purpose.

Common Mistakes

  • Thinking break exits multiple loops - it only exits the immediately enclosing loop
  • Placing code after continue in the same iteration expecting it to run - continue skips the rest of the iteration body
  • Using break/continue so heavily that the loop logic becomes hard to reason about - consider restructuring

Note

A break inside a while(true) loop is a clean way to implement a game loop or event loop: keep running until a specific condition triggers the exit. This is more readable than building a complex while condition.

Exercises

Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.

Loading editor…

Completed: 0 / 4 exercises

Knowledge Check

Question 1 of 4

What does `break` do inside a loop?