Do–while loop in C++ - Time & Space Complexity
We want to understand how the time a do-while loop takes 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.
int i = 0;
do {
// some constant time work
i++;
} while (i < n);
This code runs a loop that repeats until the variable i reaches n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The do-while loop body runs repeatedly.
- How many times: It runs exactly
ntimes, increasingieach time.
As n grows, the loop runs more times, so the total work grows in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 loop runs |
| 100 | About 100 loop runs |
| 1000 | About 1000 loop runs |
Pattern observation: The work grows evenly as n increases.
Time Complexity: O(n)
This means the time taken grows directly in proportion to the input size n.
[X] Wrong: "The do-while loop always runs at least once, so it takes constant time."
[OK] Correct: The loop runs once or more, but the total number of runs depends on n, so time grows with input size, not constant.
Understanding how loops like do-while scale helps you explain how programs behave with bigger inputs, a key skill in coding interviews.
"What if the loop increments i by 2 each time instead of 1? How would the time complexity change?"
Practice
What is the main difference between a do-while loop and a while loop in C++?
Solution
Step 1: Understand
Ado-whileloop behaviordo-whileloop executes the code block first, then checks the condition to decide if it repeats.Step 2: Compare with
Awhileloopwhileloop checks the condition first before running the code block, so it may not run at all if the condition is false initially.Final Answer:
do-whileloop runs the code block at least once before checking the condition. -> Option AQuick Check:
do-whileruns once before condition check [OK]
- Thinking while loop always runs code once
- Confusing order of condition check
- Assuming do-while and while behave the same
Which of the following is the correct syntax for a do-while loop in C++?
?
Solution
Step 1: Recall correct
The correct syntax usesdo-whilesyntaxdo { ... } while (condition);with braces and a semicolon after the while condition.Step 2: Identify syntax errors in other options
do ( /* code */ ) while condition; misses braces and semicolon; C uses incorrect keyword 'until'; A reverses order incorrectly.Final Answer:
do { /* code */ } while (condition); -> Option CQuick Check:
Syntax ends with semicolon after while(condition) [OK]
- Omitting semicolon after while(condition)
- Using 'until' instead of 'while'
- Incorrect order of do and while keywords
What will be the output of the following C++ code?
int i = 3;
do {
std::cout << i << " ";
i--;
} while (i > 0);Solution
Step 1: Trace the loop iterations
Initially, i=3. The loop prints 3, then decrements i to 2. Condition i>0 is true, so repeats printing 2, then 1.Step 2: Check when loop stops
After printing 1, i becomes 0. Condition i>0 is false, loop stops. So output is "3 2 1 "Final Answer:
3 2 1 -> Option AQuick Check:
Loop runs while i>0, prints 3,2,1 [OK]
- Including 0 in output
- Starting count from 2 instead of 3
- Printing in wrong order
Identify the error in this do-while loop code snippet:
int x = 5;
do {
std::cout << x << " ";
x++;
} while x < 10;Solution
Step 1: Check syntax of while condition
The condition afterwhilemust be enclosed in parentheses:while (x < 10);.Step 2: Verify other parts
Incrementingxis valid;dois spelled correctly; loop ends with semicolon, which is correct.Final Answer:
Missing parentheses around the condition in while statement. -> Option DQuick Check:
while condition needs parentheses [OK]
- Forgetting parentheses in while condition
- Confusing semicolon with comma at loop end
- Thinking increment is always wrong
You want to write a do-while loop that asks the user to enter a positive number and repeats until the user enters a number greater than zero. Which code snippet correctly implements this?
?
Solution
Step 1: Understand the condition to repeat
The loop should continue while the number is less than or equal to zero, so condition isnum <= 0.Step 2: Check each option's condition
int num; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num <= 0); usesdo-while (num <= 0);which repeats until user enters positive number. int num = 0; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num > 0); repeats while number is positive (wrong). int num; while (num <= 0) { std::cout << "Enter positive number: "; std::cin >> num; } uses while loop, not do-while. int num; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num < 0); repeats while number is less than zero, so zero input stops loop incorrectly.Final Answer:
int num; do { std::cout << "Enter positive number: "; std::cin >> num; } while (num <= 0); -> Option BQuick Check:
Repeat while input ≤ 0 to ensure positive input [OK]
- Using wrong condition sign (> 0 instead of ≤ 0)
- Using while loop instead of do-while
- Stopping loop too early on zero input
Ready to prove it?
Pass the test and this pattern moves unknown
