What is the difference between a while loop and a do...while loop? What do break and continue do?
Show the explanation
Model answer.
- `while (cond) { ... }` checks the condition first, so the body may run zero times.
- `do { ... } while (cond)` runs the body once before checking, so it always runs at least once. Useful for "ask until valid" input loops.
let tries = 0;
do { tries++; } while (tries < 0); // body still ran once: tries = 1- `break` leaves the loop immediately.
- `continue` skips the rest of the current iteration and moves to the next check.
for (const n of [1, 2, 3, 4, 5]) {
if (n === 2) continue; // skip 2
if (n === 4) break; // stop at 4
console.log(n); // prints 1, 3
}With nested loops, a label lets break outer; exit both loops at once.
Original UniCircle question. Last reviewed 22 Oct 2026.
