Skip to content

Day 3: Loops (for, while) & break/continue

Automate repeated tasks with for and while loops, and use break and continue control keywords.

for (let i = 1; i <= 5; i++) {
console.log(`Iteration: ${i}`);
}
let energy = 3;
while (energy > 0) {
console.log(`Running... energy: ${energy}`);
energy--;
}
  • continue: Skips current iteration and moves to next.
  • break: Immediately terminates the entire loop.
for (let i = 1; i <= 10; i++) {
if (i === 3) continue; // skip 3
if (i === 8) break; // stop at 8
console.log(i);
}
  1. Write a loop printing numbers from 1 to 20, skipping multiples of 3 using continue.
  2. Compute the sum of all numbers between 1 and 100 using a loop.