freecoding.school100% FREE · NO SIGNUP
Script SpringsISSUE #28 of 29

while · do-while

SparkyVSNaN the Trickster
Sparky sayswhile loops repeat as long as a condition holds; do-while runs at least once.

while (cond) { … } checks before each iteration; do { … } while (cond) checks after, guaranteeing one run. Use them when the iteration count isn't known up front (reading until done, retrying). Always ensure the condition eventually becomes false or you get an infinite loop.

Power-ups you unlock

NaN the Trickster attacks — common mistakes

Boss battleUse a while loop to halve a number until it drops below 1.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>let n=100, steps=[]; while(n>=1){ steps.push(n); n/=2; }
document.getElementById('o').textContent=steps.map(x=>x.toFixed(1)).join(' → ');</script></body></html>
▶ Open the interactive comic issue
‹ For...Of · Iterating ValuesBreak · Continue · Labels ›