Sobes.tech
Junior — Middle

What is the difference between the break and continue operators in loop control?

sobes.tech AI

Answer from AI

The break operator completely terminates the execution of the current loop and exits it, proceeding to the code after the loop.

The continue operator skips the remaining part of the current iteration and immediately proceeds to the next iteration.

Example in Java:

for (int i = 0; i < 5; i++) {
    if (i == 3) {
        break; // the loop will stop when i becomes 3
    }
    if (i == 1) {
        continue; // skip printing when i=1
    }
    System.out.println(i);
}
// Output will be: 0, 2