Java Break and Continue
Sometimes a loop shouldn't run in its normal rhythm. Java gives you two statements to change the flow from inside a loop:
| Statement | Effect |
|---|---|
break |
leaves the loop immediately; execution continues after it |
continue |
skips the rest of the current pass and jumps to the next one |
break
break is handy when you are searching for something and can stop as soon as you've found it:
class Main {
public static void main(String[] args) {
int[] readings = {12, 18, 25, 31, 22, 40};
int limit = 30;
for (int i = 0; i < readings.length; i++) {
if (readings[i] > limit) {
System.out.println("First value above " + limit + " at index " + i);
break;
}
System.out.println("Checked " + readings[i]);
}
System.out.println("Search finished");
}
}Checked 12
Checked 18
Checked 25
First value above 30 at index 3
Search finished
Values after index 3 were never inspected.
break with while (true)
An intentional infinite loop with a break is a common pattern when the exit condition is easiest to test in the middle of the loop:
class Main {
public static void main(String[] args) {
int n = 27;
int steps = 0;
while (true) {
if (n == 1) {
break;
}
n = (n % 2 == 0) ? n / 2 : 3 * n + 1;
steps++;
}
System.out.println("Collatz steps for 27: " + steps);
}
}Collatz steps for 27: 111
continue
continue skips to the next iteration. Here we ignore invalid (negative) values but keep processing the rest:
class Main {
public static void main(String[] args) {
int[] amounts = {50, -10, 30, -5, 20};
int total = 0;
for (int amount : amounts) {
if (amount < 0) {
System.out.println("Skipping invalid amount " + amount);
continue;
}
total += amount;
}
System.out.println("Total: " + total);
}
}Skipping invalid amount -10
Skipping invalid amount -5
Total: 100
In a for loop, continue still runs the update (i++) before checking the condition again. In a while loop, make sure the variable changes before the continue, or you may create an infinite loop.
Labeled break and continue
break and continue only affect the innermost loop. To exit an outer loop from inside a nested one, put a label (a name followed by :) before the outer loop:
class Main {
public static void main(String[] args) {
int[][] grid = {
{1, 4, 7},
{2, 9, 5},
{8, 3, 6}
};
int target = 9;
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;
}
}
}
outer:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) continue outer; // jump to next i
System.out.println(i + "," + j);
}
}
}
}Found 9 at row 1, column 1
1,1
2,1
3,1
Tip: Labels are useful but rare. If you need them often, consider moving the nested loop into its own method and using
returninstead — it's usually easier to read.
break in switch
You have already seen break in the classic switch statement, where it stops fall-through. Inside a loop, a break inside a classic switch only leaves the switch, not the loop — another reason to prefer the arrow-style switch.
Test yourself
What does continue do inside a for loop?
- Exits the loop
- Restarts the loop from the beginning
- Skips to the next iteration
- Pauses the program
What is printed? for (int i = 0; i < 5; i++) { if (i == 3) break; System.out.print(i); }
- 012
- 0123
- 01234
- 0124
How do you leave an outer loop from inside an inner loop?
- break break;
- A labeled break such as
break outer; - exit();
- It's impossible in Java