Javascript Loop Control Statements

Javascript Loop Control Statements

  • break: Exits the loop immediately.
  • continue: Bypasses the remaining code in the current loop cycle and moves to the next iteration.

Loop Control Statements

1. break

The break statement terminates the loop immediately when a condition is satisfied.

Example: Exit the loop when i equals 3

  for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        break; // Exits the loop
    }
    console.log(i);
  }

Output: 1, 2


2. continue

The continue statement bypasses the remaining code in the current iteration and jumps to the next cycle of the loop.

Example: Skip printing when i equals 3

  for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue; // Skips the rest of the loop for this iteration
    }
    console.log(i);
  }

Output: 1, 2, 4, 5


Previous Next