While and Do-While Loops0%
While and Do-While Loops
Beginner12 min read•Updated: Jul 11, 2026
While and Do-While Loops in JavaScript
While a for loop is ideal when you know in advance how many times you need to iterate, while and do...while loops are designed for situations where iteration depends on dynamic conditions (e.g. waiting for user input, reading chunks from a stream, or rolling dice until a 6 appears).
1. The while Loop: Pre-Test Loop
The while loop evaluates its condition before executing the code block. If the condition is initially false, the code block never runs at all!
Output
[ Evaluate Condition ]
|
+--------+--------+
| true | false
v v
[ Execute Body ] [ Exit Loop ]
|
+---> [ Re-evaluate Condition ]
Syntax:
JavaScript
while (condition) {
// Code executed while condition remains true
}
Practical Example: Game Loop Simulation
JavaScript
let health = 100;
let round = 1;
while (health > 0) {
const damage = Math.floor(Math.random() * 30) + 10;
health -= damage;
console.log(`Round ${round}: Player took ${damage} damage. Health left: ${Math.max(0, health)}`);
round++;
}
console.log("Game Over! Player has been defeated.");
2. The do...while Loop: Post-Test Loop
The do...while loop evaluates its condition after executing the code block.
[!IMPORTANT]
A
do...while loop is guaranteed to execute at least ONCE, even if the condition is false from the very beginning!Output
[ Execute Body First ]
|
v
[ Evaluate Condition ]
|
+--------+--------+
| true | false
v v
[ Loop repeats ] [ Exit Loop ]
Syntax:
JavaScript
do {
// Executes at least once!
} while (condition);
Practical Example: User Prompt Validation
JavaScript
let password;
do {
password = prompt("Enter a password with at least 6 characters:");
} while (!password || password.length < 6);
console.log("Password accepted!");
3. Comparison: while vs do...while
JavaScript
let count = 10;
// The while loop NEVER runs:
while (count < 5) {
console.log("This will never print!");
}
// The do...while loop runs ONCE:
do {
console.log("This prints exactly once!"); // Prints!
} while (count < 5);
| Loop Type | When Condition is Tested | Minimum Execution Count |
|---|---|---|
while | Before the loop body (Pre-test) | 0 |
do...while | After the loop body (Post-test) | 1 |
Interactive Quiz
Practice Quiz
Question 1
What is the primary operational difference between a
while loop and a do...while loop?Question 2
How many times will
let x = 5; while (x < 3) { x++; } execute?Question 3
How many times will
let y = 5; do { y++; } while (y < 3); execute?Question 4
What must you ensure occurs inside the body of a
while loop to prevent an infinite loop?Question 5
For which scenario is a
do...while loop ideal?Next Lesson
Loop Control: break, continue
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| For Loop | Loop Control: break, continue |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.