Java Break vs Continue: Key Differences

In Java, continue and break are two types of control flow statements with different functionalities.

  1. rupture
  2. Interrupt the continuous course of action.
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}

The code above will output:

0
1
2
3
4
  1. Keep going
  2. keep going
for (int i = 0; i < 10; i++) {
    if (i == 5) {
        continue;
    }
    System.out.println(i);
}

The code above will output:

0
1
2
3
4
6
7
8
9

Summary:

  1. The “break” statement is used to immediately exit the innermost loop or switch statement.
  2. The use of “continue” allows skipping the remaining code in the current iteration and moving directly to the next iteration.
bannerAds