Lesson 21 +15 XP

Break and Continue

Break and Continue

break and continue give you control inside loops.

The break statement

break exits the loop completely:

for (let i = 0; i < 10; i++) {
  if (i === 5) {
    break; // stop the loop now
  }
  console.log(i);
}

This prints 0, 1, 2, 3, 4, then stops.

The continue statement

continue skips the current iteration and moves to the next one:

for (let i = 0; i < 5; i++) {
  if (i === 2) {
    continue; // skip 2
  }
  console.log(i);
}

This prints 0, 1, 3, 4. The value 2 is skipped.

break vs continue

  • break: ends the entire loop.
  • continue: skips one iteration, the loop keeps going.

break in switch

break is also used in switch statements to stop fall through.

TL;DR

  • break ends the whole loop.
  • continue skips just the current iteration.
  • Both make loops more flexible.