Do–while loop in C - Time & Space Complexity
Let's see how the time taken by a do-while loop changes as the input size grows.
We want to know how many times the loop runs when the input gets bigger.
Analyze the time complexity of the following code snippet.
int i = 0;
do {
printf("%d\n", 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 print statement 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 the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 times |
| 100 | 100 times |
| 1000 | 1000 times |
Pattern observation: The operations increase directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time grows in a straight line with the input size; more input means more loop runs.
[X] Wrong: "The do-while loop runs one extra time compared to a while loop, so it takes more time."
[OK] Correct: Both loops run roughly the same number of times based on n; the difference is only in when the condition is checked, not the total count for this case.
Understanding how loops grow with input size helps you explain your code clearly and shows you know how to think about efficiency.
"What if we changed the loop to count down from n to 0? How would the time complexity change?"
Practice
do-while loop and a while loop in C?Solution
Step 1: Understand the execution order of loops
Awhileloop checks the condition first, so it may not run the code block if the condition is false initially.Step 2: Identify the behavior of
Ado-whileloopdo-whileloop runs the code block once before checking the condition, ensuring at least one execution.Final Answer:
Ado-whileloop executes the code block at least once before checking the condition. -> Option CQuick Check:
do-while runs once first = A [OK]
- Thinking do-while checks condition first
- Believing semicolon after while is optional
- Confusing do-while with while loop behavior
do-while loop in C?Solution
Step 1: Recall the correct syntax structure
Thedo-whileloop requires braces around the code block and a semicolon after thewhilecondition in parentheses.Step 2: Compare options to syntax
do { /* code */ } while (condition); matches the correct syntax:do { /* code */ } while (condition);with braces and semicolon.Final Answer:
do { /* code */ } while (condition); -> Option AQuick Check:
Semicolon after while(condition) = B [OK]
- Omitting semicolon after while condition
- Using parentheses instead of braces for code block
- Placing while before do
int i = 3;
do {
printf("%d ", i);
i--;
} while (i > 0);Solution
Step 1: Trace the loop iterations
Initially, i = 3. The loop prints i, then decrements i by 1. It repeats while i > 0.Step 2: List printed values
First iteration: prints 3, i becomes 2
Second iteration: prints 2, i becomes 1
Third iteration: prints 1, i becomes 0
Loop stops because i is not > 0.Final Answer:
3 2 1 -> Option DQuick Check:
Prints 3 down to 1 = A [OK]
- Including 0 in output
- Starting from 2 instead of 3
- Printing in ascending order
do-while loop code snippet:int x = 5;
do {
printf("%d", x);
x--;
} while x > 0;Solution
Step 1: Check syntax of the while condition
Thewhilecondition must be enclosed in parentheses, likewhile (x > 0);.Step 2: Identify the error in the code
The code haswhile x > 0;missing parentheses, causing a syntax error.Final Answer:
Missing parentheses around the while condition. -> Option BQuick Check:
while(condition) needs parentheses = D [OK]
- Omitting parentheses in while condition
- Forgetting semicolon after while statement
- Misplacing braces around condition
do-while loop that asks the user to enter a positive number and repeats until the user enters a positive number. Which code snippet correctly implements this?Solution
Step 1: Understand the requirement
The loop must ask for input at least once and repeat while the number is not positive (<= 0).Step 2: Analyze each option
int num; do { printf("Enter a positive number: "); scanf("%d", &num); } while (num <= 0); prompts before input and repeats while number is <= 0, which matches the requirement.
int num = 0; do { scanf("%d", &num); printf("Enter a positive number: "); } while (num > 0); prints prompt after input and repeats while number > 0, which is incorrect.
int num; do { scanf("%d", &num); } while (num < 0); repeats while number < 0, missing zero case.
int num; do { printf("Enter a positive number: "); scanf("%d", &num); } while (num < 0); repeats while number < 0, missing zero case.Final Answer:
int num; do { printf("Enter a positive number: "); scanf("%d", &num); } while (num <= 0); -> Option AQuick Check:
Repeat while number not positive = C [OK]
- Checking wrong condition (e.g., num < 0 instead of <= 0)
- Prompting after input instead of before
- Not handling zero as invalid input
Ready to prove it?
Pass the test and this pattern moves unknown
