4.5 Do...While Loops
The third looping construct in JavaScript is the do...while loop. It is close to a while loop but with one important twist: the condition is checked after the body runs, not before. Rewriting our previous example as a do...while makes the difference easy to see.
We still declare and initialise the loop variable outside the loop, for example let i = 0;. If you try to redeclare i inside, you get an error because it already exists in this scope. This is different from the i defined inside a for loop, which is block-scoped — we will come back to scope later in the course. The structure becomes:
let i = 0;
do {
if (i % 2 !== 0) console.log(i);
i++;
} while (i <= 5);
While vs do...while
- while: the condition is evaluated before each iteration, so if it is false from the start the body never runs.
- do...while: the condition is evaluated after the body, so the instructions run at least once even when the condition is false.
To illustrate, start i at 0 with a regular while (i <= 5) loop and you get 1, 3, 5 in the console. Change i to 9 and the console stays empty: the condition is false right away, so nothing executes. Now switch to the do...while version with i = 9 and you get 9 in the console. The body ran once: it printed the odd number, incremented i to 10, then the while condition was evaluated, returned false, and the loop ended.
Realistically you will not reach for do...while very often. There are situations where you want to guarantee one execution before testing a condition, but in everyday code for and while cover most cases. That's it for this short demonstration of do...while loops — see you in the next video.
Summary
This lesson introduces do...while loops, a control flow structure similar to while loops but with a critical difference in execution timing. Unlike while loops that evaluate the condition at the beginning of each iteration, do...while loops execute the code block first, then check the condition at the end, guaranteeing at least one execution regardless of whether the condition is initially true or false. The lesson demonstrates this distinction through practical code examples and explains that while do...while loops are syntactically valuable, for and while loops remain more prevalent in professional programming practice.
Key points
- do...while loops execute the code block at least once before evaluating the condition, even if the condition is false from the start
- The condition in a do...while loop is evaluated at the end of each iteration using the while keyword and a semicolon, unlike the beginning placement in while loops
- Proper syntax: do { code statements } while (condition);
- do...while loops guarantee minimum one execution, making them useful when at least one iteration is required before checking termination logic
- In practice, do...while loops are rarely used; for and while loops are more common in real-world programming scenarios
- Understanding when the condition is evaluated (start vs. end) is essential for choosing the appropriate loop type for your code
FAQ
What is the main difference between a while loop and a do...while loop?
A while loop evaluates its condition at the start of each iteration, so it may never execute if the condition is false. A do...while loop evaluates the condition at the end, guaranteeing at least one execution of the code block regardless of the condition's initial value.
Will a do...while loop execute if the condition is false?
Yes, a do...while loop always executes at least once, even if the condition is false, because the condition check happens after the code block runs on the first iteration.
When should I use a do...while loop in practical programming?
do...while loops are rarely used in professional development. They are most appropriate when you need to guarantee at least one execution of a block before checking a condition, but for and while loops fulfill most real-world looping requirements.