You’ve encountered the break statement previously in this tutorial, where it was employed to exit a switch statement prematurely.
The break statement can also facilitate exiting a loop.
In this example, the for loop terminates when i equals 4:
int i;
for (i = 0; i < 10; i++) { |
The continue statement interrupts the current iteration within the loop when a specified condition is met, proceeding directly to the next iteration.
In this example, the value 4 is skipped:
int i;
for (i = 0; i < 10; i++) { |
Break and continue can also be utilized within while loops:
int i = 0;
while (i < 10) { |
int i = 0;
while (i < 10) { |