break and continue: Controlling Loop Flow
While loops (for, while, do-while) execute their code blocks repeatedly based on a condition, sometimes you need more fine-grained control over their execution. JavaScript provides two special statements, break and continue, to alter the normal flow of a loop.
1. The break Statement
The break statement is used to immediately terminate the innermost loop (or switch statement) in which it is located. When break is encountered, the loop stops, and program execution continues with the statement immediately following the loop.
Syntax
break;
Examples
Example 1: break in a for loop
Example 2: break in a while loop
Note: break is also commonly used inside switch statements to prevent "fall-through" between cases.
2. The continue Statement
The continue statement is used to skip the current iteration of a loop and proceed to the next iteration. When continue is encountered, the rest of the code in the current loop iteration is skipped, and the loop's condition is re-evaluated (for while/do-while) or the afterthought expression is executed (for for loops).
Syntax
continue;
Examples
Example 1: continue in a for loop
Example 2: continue in a while loop
3. Key Differences: break vs. continue
break: Exits the entire loop. No more iterations will occur.continue: Skips only the current iteration. The loop continues with its next iteration (if the condition allows).
Choosing between break and continue depends on whether you want to stop the loop entirely or just bypass certain steps in specific iterations.
Exercise: break and continue Challenge
Use break and/or continue to solve the following challenges.

