Skip to content
Laviyx Learn

Try

Java course menu

Java break and continue: Controlling Loops, Including Labeled break

Learn how break exits a loop early and continue skips to the next iteration in Java, with search examples, while(true) loops, and labeled break and continue for leaving nested loops.

Beginner9 min readUpdated
On this page

break and continue change the normal flow of a loop: break leaves it immediately, and continue skips straight to the next iteration. Java also has labeled versions that work across nested loops.

break: leave the loop#

Java
public class Main {
    public static void main(String[] args) {
        int[] readings = {12, 7, 0, -3, 8, -1};

        for (int i = 0; i < readings.length; i++) {
            if (readings[i] < 0) {
                System.out.println("First negative: " + readings[i] + " at index " + i);
                break;
            }
        }
        System.out.println("Search finished");
    }
}
Output
First negative: -3 at index 3
Search finished

To know afterwards whether break happened, use a flag:

Java
public class Main {
    public static void main(String[] args) {
        int[] ids = {104, 221, 357};
        int wanted = 500;
        boolean found = false;

        for (int id : ids) {
            if (id == wanted) {
                found = true;
                break;
            }
        }
        System.out.println(found ? "Found" : "Not found");
    }
}
Output
Not found

continue: skip to the next iteration#

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

continue at the top of a loop body is a clean way to skip invalid data:

Java
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> names = List.of("Asha", "", "Ravi", "  ", "Meera");

        for (String name : names) {
            if (name.isBlank()) continue;
            System.out.println("Hello, " + name + "!");
        }
    }
}
Output
Hello, Asha!
Hello, Ravi!
Hello, Meera!

isBlank() (Java 11) is true for empty strings and strings of only spaces.

while (true) with break#

When the exit condition is in the middle of the body, an endless loop with break reads clearly:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int total = 0;
        while (true) {
            System.out.print("Number (or 'done'): ");
            String input = in.next();
            if (input.equals("done")) break;
            total += Integer.parseInt(input);
        }
        System.out.println("Total: " + total);
    }
}
Output
Number (or 'done'): 3
Number (or 'done'): 5
Number (or 'done'): done
Total: 8

Labeled break: leaving nested loops#

A plain break leaves only the innermost loop. Java lets you put a label on an outer loop and break out of it directly:

Java
public class Main {
    public static void main(String[] args) {
        int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        int target = 5;

        search:
        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid[r].length; c++) {
                if (grid[r][c] == target) {
                    System.out.println("Found " + target + " at row " + r + ", column " + c);
                    break search;   // leaves both loops
                }
            }
        }
        System.out.println("Done");
    }
}
Output
Found 5 at row 1, column 1
Done

continue label; similarly jumps to the next iteration of the labeled outer loop. Labels are useful but rare; often, moving the nested loops into a method and using return is clearer.

Common mistakes#

Practice#

Practice exercise

Print the first 5 prime numbers greater than 50. Use break to stop testing divisors once one is found, and to stop the outer loop after 5 primes.

Show solution
Java
public class Primes {
    public static void main(String[] args) {
        int found = 0;
        StringBuilder out = new StringBuilder();
        for (int n = 51; ; n++) {
            boolean prime = true;
            for (int d = 2; d * d <= n; d++) {
                if (n % d == 0) {
                    prime = false;
                    break;
                }
            }
            if (!prime) continue;
            out.append(n).append(' ');
            found++;
            if (found == 5) break;
        }
        System.out.println(out.toString().trim());
    }
}
Output
53 59 61 67 71

Key takeaway#

Finished this lesson?