Java Break vs Continue: Key Differences
In Java, continue and break are two types of control flow statements with different functionalities.
- rupture
- 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
- Keep going
- 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:
- The “break” statement is used to immediately exit the innermost loop or switch statement.
- The use of “continue” allows skipping the remaining code in the current iteration and moving directly to the next iteration.