Javascript · Loops
do...while Loops
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Run First, Check Later
A `while` loop checks its condition before running the block. If the condition is `false` from the start, the block never runs:
let hp = 0; while (hp > 0) { console.log("Still fighting!"); } console.log("while: done");
`do...while` does the opposite: it runs the block first, then checks the condition. The block is guaranteed to run at least once:
let hp = 0; do { console.log("Still fighting!"); } while (hp > 0); console.log("do...while: done");
Same condition, same value... but this time the message appears once. That's the only difference between the two loops.
Exercise 2 — Anatomy of do...while
do { // code that runs at least once } while (condition);
1. The keyword `do` followed by the block 2. The keyword `while` and the condition, after the block 3. A semicolon `;` at the very end, easy to forget!
Typical use: an action that must happen at least once and may be repeated. Here you throw a Codéball at a wild Aquachu. Each throw has a 30% chance to succeed, and you keep throwing until it's captured:
let throws = 0; let captured = false; do { throws++; console.log("Throw " + throws + "..."); captured = Math.random() < 0.3; } while (!captured); console.log("Aquachu captured in " + throws + " throw(s)!");
You always throw at least once, no matter what. Then the loop continues only while Aquachu is still free. Run it several times: the number of throws changes each time!
Exercise 3 — At Least Once
What will this code print?
let coins = 0; do { console.log("Buying a potion"); coins -= 10; } while (coins > 0); console.log(coins);
Options
• ```Buying a potion``` then ```-10```
• Only ```0```
• Nothing at all
• ```Buying a potion``` forever
Hint : Ask yourself when this kind of loop checks its condition for the first time, then evaluate it with the updated value.
Exercise 4 — Complete the do...while
Complete the loop so Zap charges its energy at least once, and keeps charging while the energy is below 50.
let energy = 10; ____ { energy += 15; console.log("Charging... " + energy); } ____ (energy ____ 50)____
Options: do while < ; for > if
Hint : Recall the two keywords of this loop and where each one goes. The very last character matters too.
Exercise 5 — while or do...while?
Both loops repeat while a condition is true. The choice comes down to one question: should the block run even if the condition is false at the start?
let hp = 100; while (hp < 100) { hp += 20; console.log("Potion!"); } console.log("Final HP: " + hp);
With a full HP bar, `while` checks first and skips the block: no potion is drunk. Now the same logic with `do...while`:
let hp = 100; do { hp += 20; console.log("Potion!"); } while (hp < 100); console.log("Final HP: " + hp);
Here the block runs before the check, so a potion is wasted and HP goes over 100. The `while` version is the right one for this logic. Pick the loop that matches what you want.
In Practice
`do...while` is the rarest of the three loops. Most of the time `for` and `while` are enough, but when you need "at least once", it's the cleanest option.
Exercise 6 — How Many Times?
How many times is a number printed?
let i = 5; do { console.log(i); i++; } while (i < 3);
Options
• ```1```
• ```0```
• ```3```
• Infinitely
Hint : Compare the starting value with the condition, then remember when this type of loop performs its first check.
Exercise 7 — From while to do...while
Here is a `while` loop:
let throws = 0; while (throws < 3) { throws++; console.log("Throw " + throws); }
Rewrite it as a `do...while` loop. The three throws must still be printed.
let throws = 0; ____ { throws++; console.log("Throw " + throws); } ____ (____ < 3);
Options: do while throws for if 3
Hint : The block stays the same; only the keywords around it move. The condition still needs to compare the right variable.
Exercise 8 — Three Loops, Three Personalities
Match each element with what it does.
Items to match
• while
• do...while
• for
Definitions
• Checks the condition before the first turn
• Runs the block at least once
• Best when the number of turns is known
Exercise 9 — Pick the Right Loop
A shop menu must be displayed at least once, then displayed again as long as the player hasn't chosen "quit". Which loop fits best?
Options
• ```do...while```
• ```for```
• ```while```
• An ```if``` statement
Hint : Look for the words that describe the minimum number of times the menu must appear.
Exercise 10 — The Healing Fountain
Florachu drinks from the healing fountain. 1. Each drink restores 30 HP and prints `"Drinking... HP: X"` 2. Florachu must drink at least once, then keeps drinking as long as its HP is below 100 3. After the loop, print `"Florachu is ready!"`
Starter code
let florachuHP = 25; // Write your loop here
Back to the course