JAVA LOOP CONTROL
Java break and continue
break and continue change the normal flow of a loop. The first statement ends the loop completely; the second skips only the current iteration.
The break statement
break immediately ends the nearest enclosing loop. The program continues with the first statement after the loop. This is useful when the desired result has been found and the remaining elements need not be checked.
This program searches a list of names for Rūta:
String[] vardai = {"Ona", "Jonas", "Rūta", "Mantas"};
for (String vardas : vardai) {
if (vardas.equals("Rūta")) {
System.out.println("Rūta rasta");
break;
}
System.out.println("Tikrinamas vardas: " + vardas);
}Tikrinamas vardas: Ona
Tikrinamas vardas: Jonas
Rūta rasta
for-eachloop takes the names in order: first Ona, then Jonas and Rūta.- Ona's name does not equal the text Rūta, so
ifblock is skipped and the name being checked is displayed. - The same happens when checking Jonas.
- When Rūta is reached, the method
equals() returnstrue. The program displays Rūta rasta. breakimmediately ends the entire loop, so Mantas, who follows Rūta, is never checked.
break is also used in the switch statement.switch block it stops execution from falling through to the next case, while in a loop it ends the repetition entirely.
The continue statement
continue ends only the current iteration. The remaining code in the loop body is skipped, but the loop continues with the next iteration.
A negative price represents an invalid entry that we do not want to display:
int[] kainos = {3, -1, 5, -1, 2};
for (int kaina : kainos) {
if (kaina < 0) {
continue;
}
System.out.println("Kaina: " + kaina + " eurai");
}Kaina: 3 eurai
Kaina: 5 eurai
Kaina: 2 eurai
- The loop receives the prices in order:
3, -1, 5, -1, 2. - The price
3is not negative, so it is displayed in the console. - Upon reaching
-1, the conditionkaina < 0is true and executescontinue. continueskips the followingprintln()line without stopping the entire loop.- Both invalid values are therefore skipped, while the prices
5and2are still displayed.
The difference between break and continue
| Statement | What happens? | Does the loop continue? |
|---|---|---|
break | The entire nearest enclosing loop ends | No |
continue | The rest of the current iteration is skipped | Yes |
break and continue in one loop
Jonas trains on weekdays but rests on Wednesday, and the loop ends when the weekend begins:
for (int diena = 1; diena <= 7; diena++) {
if (diena == 3) {
continue; // Skip training on Wednesday
}
if (diena == 6) {
break; // The weekend has started
}
System.out.println("Treniruotės diena: " + diena);
}Treniruotės diena: 1
Treniruotės diena: 2
Treniruotės diena: 4
Treniruotės diena: 5
- Neither special condition applies on the first or second day, so both days are displayed.
- On the third day, the program executes
continue. Wednesday's output is skipped, but the loop continues. - The fourth and fifth days are displayed normally.
- On the sixth day, the weekend begins and the program executes
break. - The entire loop stops, so neither the sixth nor the seventh day appears in the console.
Use in other loops
break and continue work not only in for, but also in while and do while loops:
int obuoliai = 5;
while (obuoliai > 0) {
obuoliai--;
if (obuoliai == 2) {
continue;
}
if (obuoliai == 0) {
break;
}
System.out.println("Liko obuolių: " + obuoliai);
}Liko obuolių: 4
Liko obuolių: 3
Liko obuolių: 1
- Initially there are
5apples. Their number is reduced by one at the start of each iteration. - The values
4and3are displayed normally. - When there are
2apples left,continueskips the output line, so we do not see the number 2. - In the next iteration there is
1apple left, so it is displayed. - When the value becomes
0,breakends the loop before the output.
continue.while in a loop, using continue too early can skip the counter update and create an infinite loop.
Use sparingly
These statements help end unnecessary work early and reduce deeply nested conditions. However, many break and continue statements in one loop can make its flow harder to follow, so every exit or skip condition should be clear.