JavaScript Loop Control Statements

  • This lesson explains loop control statements like break and continue.
  • Introduction to Loop Control Statements

    Loops are used to execute a block of code repeatedly.
    However, sometimes we need more control over how a loop runs.

    For example:

    • Stop a loop when a condition is met

    • Skip a specific iteration

    • Avoid unnecessary execution

    This is achieved using loop control statements.

    What Are Loop Control Statements ?

    Loop control statements are used to change the normal flow of a loop during execution.

    They allow us to:

    • Exit a loop early

    • Skip certain iterations

    • Control execution more efficiently

    Types of Loop Control Statements in JavaScript

    JavaScript provides two main loop control statements:

    1. break

    2. continue

    break Statement

    What is the break Statement ?

    The break statement is used to immediately terminate the loop, even if the loop condition is still true.

    Once break is executed:

    • The loop stops completely

    • Control moves to the statement after the loop

    Syntax of break

    break;

break Statement in for loop

Stops the loop immediately when a specific condition is met.

// Using break in for loop

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    break;
  }
  console.log(i);
}
  • Explanation

    • Loop starts from 1

    • When i becomes 3, break executes

    • Loop terminates immediately

break in while Loop

Ends the loop early when the condition inside the loop becomes true.

// Using break in while loop

let i = 1;

while (i <= 10) {
  if (i === 6) {
    break;
  }
  console.log(i);
  i++;
}
  • Real-Life Example of break

    Think of a fire alarm:

    • People keep working (loop)

    • Alarm rings (condition met)

    • Everyone stops work immediately (break)

    When to Use break ?

    • To stop searching when result is found

    • To exit loop on error condition

    • To prevent unnecessary iterations

    • In menu-driven programs

  • continue Statement

    What is the continue Statement ?

    The continue statement is used to skip the current iteration and move to the next iteration of the loop.

    Unlike break, it does not terminate the loop.

    Syntax of continue

    continue;

continue in for Loop

Skips the current iteration and moves to the next one.

// Using continue in for loop

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue;
  }
  console.log(i);
}
  • Explanation

    • When i is 3, continue skips printing

    • Loop continues with next value

continue in while Loop

Skips a specific iteration and continues with the next loop cycle.

// Using continue in while loop

let i = 0;

while (i < 5) {
  i++;

  if (i === 2) {
    continue;
  }

  console.log(i);
}
  • Important Note for while Loop

    When using continue in a while loop:

    • Make sure the update statement executes

    • Otherwise, it may cause an infinite loop

    Real-Life Example of continue

    Imagine attendance checking:

    • Teacher skips absent students

    • Continues checking remaining students

      Difference Between break and continue

      Featurebreakcontinue
      Loop terminationStops loop completelySkips one iteration
      Remaining iterationsNot executedExecuted
      Control flowMoves outside loopMoves to next loop cycle
      UsageExit loopSkip condition

    break and continue Together

    Skips certain iterations and stops the loop at a specific point.

    // Using break and continue in same loop
    
    for (let i = 1; i <= 10; i++) {
    
      if (i === 3) {
        continue;
      }
    
      if (i === 7) {
        break;
      }
    
      console.log(i);
    }
    • Common Mistakes

      • Confusing break with continue

      • Forgetting update statement with continue

      • Overusing break unnecessarily

      • Writing unclear loop conditions

      Best Practices for Loop Control

      • Use break only when necessary

      • Avoid complex logic inside loops

      • Write readable conditions

      • Test loops carefully

      • Prefer clarity over clever tricks