Sub topic 8
Exit or skip an iteration with break and continue
A loop repeats a set of statements called its body. Each pass through the body is an iteration. Sometimes you want to change what happens during an iteration: you may want to stop the loop, or skip a value and move on.
Java provides two keywords, or words with special meaning, for this:
breakends the loop immediately.continueskips the rest of the current iteration. The loop then decides whether to repeat.
Stop a loop with break
Suppose a list of readings uses -1 as a signal to stop. Once the program sees that value, it should not process any later readings.
Save this program as BreakDemo.java:
public class BreakDemo {
public static void main(String[] args) {
int[] readings = {4, 7, -1, 9};
for (int reading : readings) {
if (reading == -1) {
System.out.println("Stop signal found");
break;
}
System.out.println(reading);
}
System.out.println("After the loop");
}
}Compile and run it:
javac BreakDemo.java
java BreakDemoThe output is:
4
7
Stop signal found
After the loopThe loop prints 4 and 7. When reading is -1, break ends the loop. The value 9 is never visited. The program then continues with the statement after the loop.
break can also end a while or do-while loop. In each case, execution moves to the statement after that loop.
Skip an iteration with continue
Use continue when you want the loop to keep running but skip some work for the current value.
Save this program as ContinueDemo.java. Compile and run it in the same way as the first program, using its file and class names.
public class ContinueDemo {
public static void main(String[] args) {
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
}
System.out.println(number);
}
System.out.println("After the loop");
}
}The output is:
1
2
4
5
After the loopWhen number is 3, continue skips the print statement. It does not end the loop. The next iteration prints 4.
In a basic for loop, continue still runs the update in the loop header. Here, number++ runs before the condition is tested again. That is why the loop moves on from 3 to 4.
Use continue carefully in while loops
In a while loop, continue skips the rest of the body and returns to the condition. It does not automatically change a variable for you.
This program increases number before it may skip the print statement:
public class WhileContinueDemo {
public static void main(String[] args) {
int number = 0;
while (number < 5) {
number++;
if (number == 3) {
continue;
}
System.out.println(number);
}
}
}Save it as WhileContinueDemo.java, then compile and run it. It prints 1, 2, 4, and 5, each on its own line.
If number++ were after the if statement, the program would get stuck at 3. Each time, continue would skip the update. The condition number < 5 would stay true, causing an infinite loop: a loop that does not end on its own.
In a do-while loop, continue moves to the condition at the bottom of the loop. Check that any needed update happens before the condition is tested.
Control a loop inside another loop
A nested loop is a loop inside another loop. With an ordinary break or continue, the action applies to the nearest enclosing loop: the loop directly containing that statement.
Save and run this program as NestedControlDemo.java:
public class NestedControlDemo {
public static void main(String[] args) {
for (int row = 1; row <= 2; row++) {
for (int column = 1; column <= 4; column++) {
if (column == 2) {
continue;
}
if (row == 2 && column == 3) {
break;
}
System.out.println(row + "," + column);
}
}
System.out.println("Done");
}
}The output is:
1,1
1,3
1,4
2,1
DoneFor each row, continue skips column 2. On row 2, break ends the inner loop when the column reaches 3, so column 4 is not visited on that row. The outer loop then finishes, and the program prints Done.
Choose and trace
| Goal | Choice | What happens next |
|---|---|---|
| Stop looking after finding a value | `break` | Execution moves after the loop. |
| Ignore one value but check later values | `continue` | The current iteration ends; the loop continues. |
| Skip nothing and stop only at the normal condition | Neither | The loop follows its usual path. |
Before using either keyword, ask which loop it affects. Then trace the current value, the next update, and the next condition test.
Exercise
In a for loop that counts from 1 through 5, what prints if continue runs when the number is 4, before the print statement?
Show answer
1, 2, 3, and 5 print. The iteration for 4 skips the print statement, but the loop continues.
Exercise
In the same loop, what prints if break runs when the number is 4, before the print statement?
Show answer
1, 2, and 3 print. The break ends the loop before 4 or 5 can print.