Do–while loop in Javascript - Time & Space Complexity
We want to understand how the time it takes to run a do-while loop changes as the input size grows.
Specifically, how many times the loop runs affects the total work done.
Analyze the time complexity of the following code snippet.
let i = 0;
do {
console.log(i);
i++;
} while (i < n);
This code prints numbers from 0 up to n-1 using a do-while loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The do-while loop runs the block inside repeatedly.
- How many times: It runs exactly n times, once for each number from 0 to n-1.
As n grows, the number of times the loop runs grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows in a straight line with the input size.
Time Complexity: O(n)
This means the time to run the loop grows directly in proportion to the input size n.
[X] Wrong: "The do-while loop always runs at least once, so its time is constant regardless of n."
[OK] Correct: While the loop runs at least once, the total number of runs depends on n, so time grows with n.
Understanding how loops like do-while scale helps you explain how your code handles bigger inputs clearly and confidently.
"What if the loop condition was based on a fixed number instead of n? How would the time complexity change?"
Practice
do-while loop and a while loop in JavaScript?Solution
Step 1: Understand
Ado-whileloop behaviordo-whileloop executes the code block first, then checks the condition to decide if it should repeat.Step 2: Compare with
Awhileloopwhileloop checks the condition before running the code block, so it may not run at all if the condition is false initially.Final Answer:
Ado-whileloop runs the code block at least once before checking the condition. -> Option BQuick Check:
do-whileruns once before condition [OK]
- Thinking while loop always runs once
- Believing do-while skips condition check
- Confusing order of execution and condition check
do-while loop in JavaScript?Solution
Step 1: Recall correct
Thedo-whilesyntaxdo-whileloop starts withdo, then the code block in braces, followed bywhilewith the condition in parentheses and a semicolon.Step 2: Check each option
do { /* code */ } while (condition); matches the correct syntax exactly. Others have wrong order or missing parts.Final Answer:
do { /* code */ } while (condition); -> Option CQuick Check:
Syntax: do { } while(condition); [OK]
- Forgetting the semicolon after while(condition)
- Placing while before do
- Using parentheses incorrectly around code block
let count = 3;
do {
console.log(count);
count--;
} while (count > 0);Solution
Step 1: Trace the loop iterations
Initial count is 3. The loop prints count, then decreases it by 1. It repeats while count > 0.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.Final Answer:
3 2 1 -> Option AQuick Check:
Prints 3,2,1 then stops [OK]
- Including 0 in output
- Printing after decrement instead of before
- Confusing loop condition direction
do-while loop code:let i = 0;
do {
console.log(i);
i++
} while i < 3;Solution
Step 1: Check the
The condition afterwhilecondition syntaxwhilemust be inside parentheses. Here, it is written aswhile i < 3;without parentheses.Step 2: Verify other parts
Missing semicolon afteri++is allowed in JavaScript (automatic semicolon insertion). Variableiis initialized. Condition does not go inside braces, only parentheses.Final Answer:
Missing parentheses around the condition in while statement. -> Option DQuick Check:
Condition must be in ( ) after while [OK]
- Omitting parentheses around while condition
- Thinking semicolon inside loop is mandatory
- Putting condition inside curly braces
do-while loop code correctly repeats the prompt until the user enters a valid number?Solution
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.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; uninitializednumfine 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.Final Answer:
let num; do { num = prompt('Enter a number > 10'); } while (num <= 10); -> Option AQuick Check:
Repeat while input <= 10 [OK]
- Using wrong comparison operator
- Loop condition reversed
- Initializing variable unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
