Java Break and Continue

Java break and continue are control-flow statements that change what happens inside a loop. break stops the nearest loop or switch, while continue skips the rest of the current loop step and moves to the next one.

These statements are useful when a loop needs an early exit or when one value should be skipped without stopping the whole loop.

Using Break In A Loop

The break statement exits the nearest enclosing loop immediately. Java then continues with the first statement after the loop.

public class Main {
    public static void main(String[] args) {
        for (int number = 1; number <= 5; number++) {
            if (number == 4) {
                break;
            }

            System.out.println(number);
        }

        System.out.println("Loop ended");
    }
}

Output:

1
2
3
Loop ended

The loop starts at 1 and prints each number. When number becomes 4, the if condition is true, so break exits the loop before 4 is printed.

Using Continue In A Loop

The continue statement does not stop the whole loop. It skips the remaining code in the current iteration, then the loop moves on to its next check or update.

public class Main {
    public static void main(String[] args) {
        for (int number = 1; number <= 5; number++) {
            if (number == 3) {
                continue;
            }

            System.out.println(number);
        }
    }
}

Output:

1
2
4
5

When number is 3, Java runs continue and skips the System.out.println statement for that iteration. The loop still continues with 4 and 5.

Break With While Loops

break is often used in a while loop when the stopping point is discovered inside the loop body. This can make the loop stop as soon as a target value is found.

public class Main {
    public static void main(String[] args) {
        int[] scores = {72, 81, 95, 68};
        int index = 0;

        while (index < scores.length) {
            if (scores[index] >= 90) {
                System.out.println("Found high score: " + scores[index]);
                break;
            }

            index++;
        }
    }
}

Output:

Found high score: 95

The loop checks each score in order. When it finds 95, it prints the message and break stops the loop, so there is no need to check the remaining values.

Continue With While Loops

In a while loop, be careful to update the loop variable before continue when needed. If you skip the update, the loop may get stuck on the same value forever.

public class Main {
    public static void main(String[] args) {
        int number = 0;

        while (number < 5) {
            number++;

            if (number == 2) {
                continue;
            }

            System.out.println(number);
        }
    }
}

Output:

1
3
4
5

The update number++ happens before the continue check. That means the loop can safely skip printing 2 and still move forward.

Break In Switch Statements

You have already seen break in switch statements. There, break exits the switch after a matching case runs, preventing Java from continuing into the next case.

public class Main {
    public static void main(String[] args) {
        String size = "M";

        switch (size) {
            case "S":
                System.out.println("Small");
                break;
            case "M":
                System.out.println("Medium");
                break;
            case "L":
                System.out.println("Large");
                break;
            default:
                System.out.println("Unknown size");
        }
    }
}

Output:

Medium

Break Or Continue?

Statement What it does Common use
break Stops the nearest loop or switch. Exit early after finding a result.
continue Skips the rest of the current loop iteration. Ignore one value and keep looping.

Common Mistakes

  • Using break when you only meant to skip one item.
  • Using continue in a while loop before updating the loop variable.
  • Forgetting that break only exits the nearest loop, not every nested loop.
  • Leaving out break in a switch when fall-through was not intended.

Takeaway: use break to leave a loop or switch early, and use continue to skip one loop iteration while keeping the loop going.