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);
}
Console output

Tikrinamas vardas: Ona
Tikrinamas vardas: Jonas
Rūta rasta

  1. for-each loop takes the names in order: first Ona, then Jonas and Rūta.
  2. Ona's name does not equal the text Rūta, so if block is skipped and the name being checked is displayed.
  3. The same happens when checking Jonas.
  4. When Rūta is reached, the method returns true. The program displays Rūta rasta.
  5. break immediately 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");
}
Console output

Kaina: 3 eurai
Kaina: 5 eurai
Kaina: 2 eurai

  1. The loop receives the prices in order: 3, -1, 5, -1, 2.
  2. The price 3 is not negative, so it is displayed in the console.
  3. Upon reaching -1, the condition kaina < 0 is true and executes continue.
  4. continue skips the following println() line without stopping the entire loop.
  5. Both invalid values are therefore skipped, while the prices 5 and 2 are still displayed.

The difference between break and continue

Java break and continue comparison
StatementWhat happens?Does the loop continue?
breakThe entire nearest enclosing loop endsNo
continueThe rest of the current iteration is skippedYes

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);
}
Console output

Treniruotės diena: 1
Treniruotės diena: 2
Treniruotės diena: 4
Treniruotės diena: 5

  1. Neither special condition applies on the first or second day, so both days are displayed.
  2. On the third day, the program executes continue. Wednesday's output is skipped, but the loop continues.
  3. The fourth and fifth days are displayed normally.
  4. On the sixth day, the weekend begins and the program executes break.
  5. 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);
}
Console output

Liko obuolių: 4
Liko obuolių: 3
Liko obuolių: 1

  1. Initially there are 5 apples. Their number is reduced by one at the start of each iteration.
  2. The values 4 and 3 are displayed normally.
  3. When there are 2 apples left, continue skips the output line, so we do not see the number 2.
  4. In the next iteration there is 1 apple left, so it is displayed.
  5. When the value becomes 0, break ends the loop before the output.
Update the condition's state before 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.