Java Break vs Continue: Key Differences
In Java, both break and continue are control flow statements used to control the execution flow of loops or conditional statements, but they serve different purposes.
- – fracture
- change
- change
- fracture
- change or swap
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
In the above example, when i is equal to 5, the ‘break’ statement will terminate the loop execution and no longer output the subsequent numbers.
- Keep going.
- keep going
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
In the example above, when i is an even number, the continue statement will skip the execution of System.out.println(i); and go directly to the next iteration.