Do-while loop execution model in C Sharp (C#) - Time & Space Complexity
We want to understand how the time a do-while loop takes changes as the input size grows.
How many times does the loop run when the input gets bigger?
Analyze the time complexity of the following code snippet.
int i = 0;
do
{
Console.WriteLine(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 loop runs more times, directly matching n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of operations grows in a straight line with n.
Time Complexity: O(n)
This means the time taken grows directly in proportion to the input size.
[X] Wrong: "A do-while loop always runs at least twice, so it has constant time."
[OK] Correct: The do-while loop runs at least once, but the total runs depend on n, so time grows with input size.
Understanding how loops like do-while behave helps you explain how your code scales, a key skill in programming.
"What if the loop condition changed to run while i <= n? How would the time complexity change?"
Practice
do-while loop and a while loop in C#?Solution
Step 1: Understand
Thedo-whilebehaviordo-whileloop runs the code block first, then checks the condition to decide if it repeats.Step 2: Compare with
Thewhileloopwhileloop checks the condition first before running the code block, so it may not run at all.Final Answer:
Thedo-whileloop executes the code block at least once before checking the condition. -> Option AQuick Check:
do-whileruns once before condition check [OK]
- Thinking while loop runs code before condition check
- Believing do-while never checks condition
- Confusing the order of execution
do-while loop in C#?Solution
Step 1: Recall correct
Thedo-whilesyntaxdo-whileloop syntax is:do { /* code */ } while (condition);Step 2: Identify incorrect options
Options A, B, and D have incorrect order or missing parentheses/braces.Final Answer:
do { /* code */ } while (condition); -> Option BQuick Check:
Syntax: do { } while (condition); [OK]
- Omitting semicolon after while condition
- Placing while before do
- Using parentheses incorrectly
int count = 3;
do {
Console.Write(count + " ");
count--;
} while (count > 0);Solution
Step 1: Trace the loop execution
Initial count is 3. The loop prints count, then decrements it. It repeats while count > 0.Step 2: List printed values
First iteration: prints 3, count becomes 2.
Second: prints 2, count becomes 1.
Third: prints 1, count becomes 0.
Loop stops because count is not > 0.Final Answer:
3 2 1 -> Option DQuick Check:
Prints 3,2,1 then stops [OK]
- Including 0 in output
- Printing after decrement instead of before
- Confusing loop condition
do-while loop code snippet:int i = 0;
do {
Console.Write(i);
i++;
} while i < 5;Solution
Step 1: Check syntax of while condition
The condition in ado-whileloop must be enclosed in parentheses:while (condition);Step 2: Identify missing parentheses
The code haswhile i < 5;without parentheses, causing a syntax error.Final Answer:
Missing parentheses around the condition in the while statement. -> Option CQuick Check:
Condition must be in parentheses [OK]
- Forgetting parentheses around condition
- Missing semicolon after while statement
- Assuming variable initialization error
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 loop condition needed
The loop should repeat while the number is not positive, i.e., while number is less than or equal to zero.Step 2: Check each option's condition
int num; do { Console.WriteLine("Enter a positive number:"); num = int.Parse(Console.ReadLine()); } while (num <= 0); useswhile (num <= 0);which repeats until user enters a positive number.
Other options have incorrect conditions that either stop too early or never repeat correctly.Final Answer:
int num; do { Console.WriteLine("Enter a positive number:"); num = int.Parse(Console.ReadLine()); } while (num <= 0); -> Option AQuick Check:
Repeat while number ≤ 0 to ensure positivity [OK]
- Using wrong comparison operators in condition
- Initializing variable incorrectly
- Stopping loop too early or never
Ready to prove it?
Pass the test and this pattern moves unknown
