Bird
Raised Fist0
Javascriptprogramming~10 mins

Do–while loop in Javascript - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Do–while loop
Start
↓
Execute loop body
↓
Check condition
Yes| No↓
Repeat
The do–while loop runs the code inside the loop first, then checks the condition. If true, it repeats; if false, it stops.
Execution Sample
Javascript
let count = 1;
do {
  console.log(count);
  count++;
} while (count <= 3);
This code prints numbers 1 to 3 using a do–while loop.
Execution Table
Stepcount before bodyActioncount after bodyCondition (count <= 3)Loop continues?
11Print 1, increment count to 222 <= 3Yes
22Print 2, increment count to 333 <= 3Yes
33Print 3, increment count to 444 <= 3No
💡 At step 3, count becomes 4, condition 4 <= 3 is false, loop stops.
Variable Tracker
VariableStartAfter 1After 2After 3Final
count12344
Key Moments - 3 Insights
Why does the loop body run even when the condition is false at the start?
Because the do–while loop runs the body first before checking the condition, as shown in step 1 of the execution_table.
When does the condition get checked in a do–while loop?
The condition is checked after the loop body runs, as seen in the 'Condition' column after each action in the execution_table.
What happens if the condition is false after the first iteration?
The loop stops after completing the first iteration, because the condition check fails, shown in step 3 where loop continues is 'No'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count before the second loop body execution?
A1
B2
C3
D4
💡 Hint
Check the 'count before body' column at step 2 in the execution_table.
At which step does the loop condition become false?
AStep 1
BStep 2
CStep 3
DThe condition never becomes false
💡 Hint
Look at the 'Condition' and 'Loop continues?' columns in the execution_table.
If the initial count was 4, how many times would the loop body run?
A1 time
B0 times
C3 times
DInfinite times
💡 Hint
Remember do–while runs the body once before checking condition; see key_moments about first iteration.
Concept Snapshot
do {
  // code to run
} while (condition);

- Runs loop body first, then checks condition.
- Repeats if condition is true.
- Always runs at least once.
Full Transcript
A do–while loop in JavaScript runs the code inside the loop first, then checks the condition. If the condition is true, it repeats the loop. This means the loop body always runs at least once, even if the condition is false at the start. For example, if count starts at 1, the loop prints count and increments it, then checks if count is less than or equal to 3. It repeats until count becomes 4, then stops. This is different from a while loop that checks the condition before running the body. The execution table shows each step: the value of count before running the body, the action taken, the new count, the condition check, and whether the loop continues. Beginners often wonder why the loop runs once even if the condition is false initially; this is because the condition is checked after the body runs. If the initial count was 4, the loop would still run once before stopping. This behavior makes do–while useful when you want the loop to run at least once no matter what.

Practice

(1/5)
1. What is the main difference between a do-while loop and a while loop in JavaScript?
easy
A. A while loop always runs the code block at least once.
B. A do-while loop runs the code block at least once before checking the condition.
C. A do-while loop never checks the condition.
D. A while loop runs the code block only if the condition is false.

Solution

  1. Step 1: Understand do-while loop behavior

    A do-while loop executes the code block first, then checks the condition to decide if it should repeat.
  2. Step 2: Compare with while loop

    A while loop checks the condition before running the code block, so it may not run at all if the condition is false initially.
  3. Final Answer:

    A do-while loop runs the code block at least once before checking the condition. -> Option B
  4. Quick Check:

    do-while runs once before condition [OK]
Hint: Remember: do-while runs code first, then checks condition [OK]
Common Mistakes:
  • Thinking while loop always runs once
  • Believing do-while skips condition check
  • Confusing order of execution and condition check
2. Which of the following is the correct syntax for a do-while loop in JavaScript?
easy
A. do (condition) { /* code */ };
B. while (condition) { /* code */ } do;
C. do { /* code */ } while (condition);
D. while { /* code */ } do (condition);

Solution

  1. Step 1: Recall correct do-while syntax

    The do-while loop starts with do, then the code block in braces, followed by while with the condition in parentheses and a semicolon.
  2. Step 2: Check each option

    do { /* code */ } while (condition); matches the correct syntax exactly. Others have wrong order or missing parts.
  3. Final Answer:

    do { /* code */ } while (condition); -> Option C
  4. Quick Check:

    Syntax: do { } while(condition); [OK]
Hint: Syntax always: do { } while(condition); ends with semicolon [OK]
Common Mistakes:
  • Forgetting the semicolon after while(condition)
  • Placing while before do
  • Using parentheses incorrectly around code block
3. What will be the output of the following code?
let count = 3;
do {
  console.log(count);
  count--;
} while (count > 0);
medium
A. 3 2 1
B. 3 2 1 0
C. 2 1 0
D. 1 2 3

Solution

  1. Step 1: Trace the loop iterations

    Initial count is 3. The loop prints count, then decreases it by 1. It repeats while count > 0.
  2. Step 2: List printed values

    First print: 3, count becomes 2
    Second print: 2, count becomes 1
    Third print: 1, count becomes 0
    Loop stops because count is not > 0.
  3. Final Answer:

    3 2 1 -> Option A
  4. Quick Check:

    Prints 3,2,1 then stops [OK]
Hint: Count down from 3, print before decrement, stop when zero [OK]
Common Mistakes:
  • Including 0 in output
  • Printing after decrement instead of before
  • Confusing loop condition direction
4. Identify the error in this do-while loop code:
let i = 0;
do {
  console.log(i);
  i++
} while i < 3;
medium
A. Missing semicolon after i++ inside the loop.
B. The loop condition should be inside curly braces.
C. Variable i is not initialized.
D. Missing parentheses around the condition in while statement.

Solution

  1. Step 1: Check the while condition syntax

    The condition after while must be inside parentheses. Here, it is written as while i < 3; without parentheses.
  2. Step 2: Verify other parts

    Missing semicolon after i++ is allowed in JavaScript (automatic semicolon insertion). Variable i is initialized. Condition does not go inside braces, only parentheses.
  3. Final Answer:

    Missing parentheses around the condition in while statement. -> Option D
  4. Quick Check:

    Condition must be in ( ) after while [OK]
Hint: Always put condition in parentheses after while [OK]
Common Mistakes:
  • Omitting parentheses around while condition
  • Thinking semicolon inside loop is mandatory
  • Putting condition inside curly braces
5. You want to prompt a user to enter a number greater than 10. Which do-while loop code correctly repeats the prompt until the user enters a valid number?
hard
A. let num; do { num = prompt('Enter a number > 10'); } while (num <= 10);
B. let num = 0; do { num = prompt('Enter a number > 10'); } while (num < 10);
C. let num; do { num = prompt('Enter a number > 10'); } while (num < 10);
D. let num; do { num = prompt('Enter a number > 10'); } while (num > 10);

Solution

  1. Step 1: Understand the condition needed

    The loop should continue prompting while the entered number is less than or equal to 10. Since prompt returns a string, comparison with 10 works if user enters a number string.
  2. Step 2: Check each option

    let num; do { num = prompt('Enter a number > 10'); } while (num <= 10);: while (num <= 10) - correct condition to reject 10 or less; uninitialized num fine in do-while.
    let num = 0; do { num = prompt('Enter a number > 10'); } while (num < 10);: while (num < 10) - accepts 10 incorrectly; unnecessary init.
    let num; do { num = prompt('Enter a number > 10'); } while (num < 10);: while (num < 10) - accepts 10 incorrectly.
    let num; do { num = prompt('Enter a number > 10'); } while (num > 10);: while (num > 10) - loops when input is valid.
  3. Final Answer:

    let num; do { num = prompt('Enter a number > 10'); } while (num <= 10); -> Option A
  4. Quick Check:

    Repeat while input <= 10 [OK]
Hint: Repeat prompt while input <= 10 [OK]
Common Mistakes:
  • Using wrong comparison operator
  • Loop condition reversed
  • Initializing variable unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard