Curriculum
Concept 20 // Module 02: Values, Operators & Control Flow
Loops: for, while, do...while, break & continue
Module 02: Values, Operators & Control Flow•Concept 20 of 100• 5 min read
Loops: for, while, do...while, break & continue
Loop statements repeat instructions while a condition holds true. `for` provides index iteration, `while` tests before each run, and `do...while` guarantees at least one execution.
The Core Concept
Syntax Reference
for (let i = 0; i < 5; i++) {
if (i === 2) continue; // skip 2
if (i === 4) break; // exit loop
}
while (condition) {
// runs while condition is true
}
do {
// runs at least once
} while (condition);Live Interactive Studio
`continue` immediately advances to the next iteration. `break` terminates the entire loop.
JavaScript Editor
Loading...
Console Output
Waiting for output...
Key Code Patterns in Action
01
Loop Flow Control with break and continue
const items = [10, 20, -1, 30, 40];
const results = [];
for (let i = 0; i < items.length; i++) {
if (items[i] < 0) {
continue; // Skip negative numbers
}
if (items[i] > 35) {
break; // Stop completely if over 35
}
results.push(items[i]);
}
console.log("Processed:", results); // [10, 20, 30]`continue` immediately advances to the next iteration. `break` terminates the entire loop.
02
Guaranteed Execution with do...while
let attempts = 0;
do {
attempts++;
console.log("Execution attempt:", attempts);
} while (attempts < 1);`do...while` evaluates its condition after the block body, guaranteeing that the body executes at least once.
03
1. Core Usage: Loops
// Core syntax and usage for Loops: for, while, do...while, break & continue
const feature = 'loops-for-while-do-while-break-continue';
console.log('Concept 20:', feature);
console.log('Active:', true);Illustrates the fundamental invocation and syntax structure for Loops: for, while, do...while, break & continue.
04
2. Defensive Handling & Edge Case
// Edge-case defensive handling
function handleSafe(input) {
if (input === null || input === undefined) return 'safe-fallback';
return String(input).trim();
}
console.log(handleSafe('loops-for-while-do-while-break-continue'));
console.log(handleSafe(null));Shows how to write defensive, safe JavaScript code that handles nullish or unexpected inputs when working with Loops: for, while, do...while, break & continue.
05
3. Clean Architecture & Production Pattern
// Production-grade pattern for Loops: for, while, do...while, break & continue
const settings = Object.freeze({
id: 'loops-for-while-do-while-break-continue',
ready: true,
timestamp: Date.now()
});
console.log('Settings initialized:', settings.id, settings.ready);Demonstrates clean, immutable production code avoiding unintended state mutations.
Real-World Engineering Takeaways
Array Processing: Performing high-throughput numeric array loops.
Polling: Retrying a connection or checking for a condition with `while`.
Data Transformation: Rebuilding nested structures efficiently.
Under the Hood: Engine & Memory Mechanics
How JavaScript Executes This
Modern JIT engines apply loop unrolling and induction variable optimizations, hoisting invariants outside the loop body to maximize CPU instruction cache efficiency.
Common Pitfalls & Traps
Beware of infinite loops with while conditions
let i = 0; while (i < 5) { /* forgot i++! */ } // Freezes threadA while loop whose termination condition is never updated will hang the JavaScript main thread permanently.
Quick Knowledge Check
Test your understanding on these 10 questions (one by one):
Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model
What is the core purpose of Loops: for, while, do...while, break & continue in JavaScript?
// Concept 20: Loops: for, while, do...while, break & continue
// Understanding the core mental model1 / 10