While and Do-While Loops
for loops shine when you know exactly how many times to iterate. But what if you want to keep looping until something happens? That's where while and do...while loops come in. They repeat as long as a condition stays true—perfect for waiting on user input, retrying operations, or processing data until it's finished.
Step 1: while Loop Basics
let streak = 0;
while (streak < 5) {
streak++;
console.log(`Streak day ${streak}!`);
}- Checks the condition (
streak < 5) before each iteration. - If the condition is false at the start, the loop never runs.
- Make sure something in the loop changes the condition—otherwise you'll create an infinite loop.
Use cases
- Reading data until you reach the end.
- Waiting for a flag (e.g.,
isLoaded) to become true. - Running a process while a queue has items.
Step 2: Translating for to while
Most for loops can be rewritten as while loops:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}This flexibility helps when you need a loop but the number of iterations isn't predetermined.
Step 3: do...while Loop
do...while guarantees the loop body runs at least once, even if the condition starts false.
let attempts = 0;
do {
attempts++;
console.log(`Attempt ${attempts} logged.`);
} while (attempts < 3);- The condition is checked after the loop body.
- Great for scenarios where you want to execute code first, then decide whether to continue (e.g., prompting a user, rolling dice, retrying a request).
Step 4: Choosing Between while and do...while
| Scenario | Use |
|---|---|
| Need to check condition before running | while |
| Must run code at least once | do...while |
| Known number of iterations | for (still works with while) |
Example: asking for feedback until the user submits something
let feedback = "";
do {
feedback = "Great lesson!"; // imagine this comes from user input
console.log("Thanks for your feedback!");
} while (!feedback);Step 5: Avoiding Infinite Loops
Always adjust variables inside the loop so your condition eventually becomes false.
let energy = 3;
while (energy > 0) {
console.log("Practicing coding...");
energy--;
}
console.log("Time for a break! ☕");- If you forget
energy--, the loop never ends. - Use safeguards (like
if (energy < 0) break;) when working with external data.
Step 6: while + Arrays
You can iterate through arrays with while, especially when working with queues:
const reminders = ["Stretch", "Drink water", "Review notes"];
let index = 0;
while (index < reminders.length) {
console.log(`Reminder: ${reminders[index]}`);
index++;
}Or mutate arrays until empty:
const tasks = ["Refactor code", "Write tests", "Ship feature"];
while (tasks.length > 0) {
const task = tasks.shift(); // removes first item
console.log(`Completed: ${task}`);
}Step 7: Realistic Example—Countdown with do...while
let countdown = 5;
do {
console.log(`Countdown: ${countdown}`);
countdown--;
} while (countdown > 0);
console.log("Liftoff!");Even if countdown starts at 0, you'd still see at least one log—sometimes that's exactly what you want.
Step 8: Practice Prompts
- Use a
whileloop to simulate saving coins until you reach 200 units. Start withlet savings = 0;and add 25 each time. - Create a
do...whileloop that rolls a die (useMath.ceil(Math.random() * 6)) until you roll a 6. Count how many attempts it took. - Given
const messages = ["Keep going", "You're almost there", "Celebrate progress"];, log each message, then remove it from the array usingshift()until none remain. - Implement a
whileloop that keeps doubling a number until it exceeds 1000. Log each intermediate value.
Key Takeaways
- ✅
whileloops repeat as long as a condition stays true, checking before each run. - ✅
do...whileloops run once before checking the condition. - ✅ Update loop variables to avoid infinite loops.
- ✅ Choose the loop that fits your scenario: known count (
for), conditional repetition (while), guaranteed first run (do...while).
🎯 Quick Check
- When does a
do...whileloop check its condition? - How would you convert a
forloop into awhileloop? - Why is
while (true)dangerous, and how can you prevent infinite loops? - Describe a scenario where
do...whileis more appropriate thanwhile.
Next lesson: controlling loops with break and continue for even more precision. 🛑➡️