Loops
Understand how for, while, and do...while loops repeat code, and how break and continue control that repetition.
Loops let a program repeat a block of code multiple times without duplicating it by hand — essential for processing lists of data, repeating an action a set number of times, or waiting until a condition changes.
The for loop
A for loop packs three pieces into one line: an initializer (runs once before the loop starts), a condition (checked before every iteration — the loop keeps going as long as this is truthy), and an update expression (runs after every iteration).
for loops are the natural choice when you know, or can compute, how many times you want to repeat something — most commonly when counting up or down through a range of numbers or iterating over array indexes.
The while loop
A while loop checks its condition before each iteration and keeps running the block as long as that condition stays truthy. It has no built-in initializer or update step — you are responsible for setting up any variable beforehand and changing it inside the loop body.
while is a good fit when the number of iterations is not known ahead of time — for example, reading data until a signal to stop appears, or repeating an action until some external condition becomes true.
The do...while loop
A do...while loop is almost identical to while, with one key difference: it checks its condition after running the block, not before. This guarantees the loop body executes at least once, even if the condition is falsy from the very start.
This makes do...while useful for situations like a menu prompt or retry logic, where you want an action to happen at least one time before deciding whether to repeat it.
break: exiting a loop early
The break statement immediately stops the nearest enclosing loop (or switch) and jumps to the code right after it, regardless of what the loop's condition currently says. It is commonly used once a search has found what it was looking for, so the loop does not waste time checking the rest.
continue: skipping to the next iteration
The continue statement skips the rest of the current iteration's code and jumps straight to the next iteration's condition check (and, in a for loop, its update expression) — it does not exit the loop, unlike break.
continue is commonly used to skip values that do not meet some criteria, without needing to wrap the remaining logic in an extra if block.
The three loop forms
for combines initializer, condition, and update into one line — best when the iteration count is known. while checks its condition before the body runs, so the body might run zero times. do...while checks its condition after the body runs, so the body always runs at least once.
// for
for (let i = 0; i < 5; i++) {
console.log(i);
}
// while
let n = 0;
while (n < 5) {
console.log(n);
n++;
}
// do...while
let m = 0;
do {
console.log(m);
m++;
} while (m < 5);Code Examples
i starts at 1, the loop runs while i <= 5 stays true, and i++ increases i by one after every iteration, producing five lines of output. Expected result: Count: 1 Count: 2 Count: 3 Count: 4 Count: 5 i++ runs after each iteration's body, right before the condition (i <= 5) is checked again.
For every i, even numbers are skipped with continue (jumping straight to the next iteration), while the loop stops completely with break as soon as i exceeds 7 — so 9 is never even checked for evenness. Expected result: 1 3 5 7 Notice 9 never appears: once i becomes 8 it is skipped by continue (even), and once i becomes 9, break has already exited the loop at i = 9 checking i > 7... actually walk through it: i=9 is odd so continue is not triggered, then i > 7 is true (9 > 7), so break fires before printing 9.
continue skips only the current iteration; break exits the entire loop immediately.
for (let i = 1; i <= 5; i++) {
console.log("Count: " + i);
}for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
continue; // skip even numbers
}
if (i > 7) {
break; // stop entirely once i passes 7
}
console.log(i);
}for loop execution order
The condition is always checked before each iteration's body runs; the update step runs after the body, right before the next check.
for (let i = 0; i < 3; i++) {
console.log(i);
}
init: i = 0
↓
┌─→ check: i < 3? ──false──→ exit loop
│ │true
│ ↓
│ run body (console.log(i))
│ ↓
└── update: i++Important Rules
for is best when the number of iterations is known or computable in advance.
while checks its condition before running the body, so the body may run zero times.
do...while checks its condition after running the body, guaranteeing at least one execution.
break exits the nearest enclosing loop entirely; continue skips only to the next iteration.
Forgetting to update the loop's controlling variable inside a while or do...while creates an infinite loop.
Where this shows up
Rendering a list of items from an array, retrying a network request a fixed number of times, processing rows from a database query result, or animating something frame by frame all rely on loops.
let i = 0; while (i < 5) { console.log(i); } let i = 0; while (i < 5) { console.log(i); i++; }
The original code never changes i, so the condition i < 5 stays true forever, producing an infinite loop that will freeze or crash the program.
for (let i = 0; i < arr.length; i++) { if (arr[i] === target) break; console.log("checking", arr[i]); } console.log("found at", i);
for (let i = 0; i < arr.length; i++) { if (arr[i] === target) { console.log("found at", i); break; } } let inside a for loop's initializer is scoped to the loop and does not exist outside it, so referencing i after the loop throws a ReferenceError; use the value inside the loop (or declare a variable outside it) instead.
[Multiple Choice] Which loop is guaranteed to run its body at least once, even if the condition is false from the start? A. for B. while C. do...while D. None of them
C. do...while — do...while checks its condition after executing the body, so the body always runs once before the condition is ever evaluated. for and while both check their condition first and may never run their body.
[True / False] The continue statement stops the loop entirely, just like break. A. True B. False
B. False — False. continue skips only the rest of the current iteration and moves on to the next one; break is the statement that exits the loop entirely.
Write a for loop that prints only the numbers from 1 to 20 that are divisible by 3, and stops entirely once it reaches a number greater than 15.
for (let i = 1; i <= 20; i++) { if (i > 15) { break; } if (i % 3 !== 0) { continue; } console.log(i); }
Use continue to skip numbers not divisible by 3 (number % 3 !== 0), and break once the number exceeds 15.