Do-while loop execution model in PHP - 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 does the loop run when the input gets bigger?
Analyze the time complexity of the following code snippet.
$i = 0;
do {
echo $i . "\n";
$i++;
} while ($i < $n);
This code prints numbers from 0 up to one less than n using a do-while loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The do-while loop that prints and increments the counter.
- How many times: It runs exactly n times, once for each number from 0 to n-1.
As n grows, the number of loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop runs |
| 100 | 100 loop runs |
| 1000 | 1000 loop runs |
Pattern observation: The number of operations grows directly with n, so doubling n doubles the work.
Time Complexity: O(n)
This means the time taken grows in a straight line with the input size.
[X] Wrong: "The do-while loop runs one more time than n because it checks after the first run."
[OK] Correct: The do-while loop runs exactly n times here, not more or less, because the condition is checked after each iteration and the loop stops when $i is no longer less than n.
Understanding how loops like do-while behave helps you explain how your code scales and shows you can think about performance clearly.
"What if we changed the loop to run while $i <= $n instead of $i < $n? How would the time complexity change?"
Practice
do-while loop and a while loop in PHP?Solution
Step 1: Understand the execution order of
Ado-whileloopdo-whileloop runs the code block first, then checks the condition to decide if it should repeat.Step 2: Compare with
Awhileloop behaviorwhileloop 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 executes the code block at least once before checking the condition. -> Option BQuick Check:
do-whileruns code once before condition check [OK]
- Thinking while loop runs code before condition check
- Believing do-while skips condition check
- Confusing execution order of loops
do-while loop in PHP?Solution
Step 1: Recall PHP
The correct syntax is:do-whilesyntaxdo { /* code */ } while (condition);with a semicolon after the while condition.Step 2: Identify incorrect options
Options A, C, and D use wrong keywords or misplaced parentheses, which are invalid in PHP.Final Answer:
do { /* code */ } while (condition); -> Option AQuick Check:
Correctdo-whilesyntax ends with semicolon [OK]
- Omitting semicolon after while condition
- Using 'until' instead of 'while'
- Placing condition before code block
$i = 3;
do {
echo $i . ' ';
$i--;
} while ($i > 0);Solution
Step 1: Trace the loop iterations
Initial value: $i = 3. Loop prints $i then decrements it. Iterations:
1) print 3, $i=2
2) print 2, $i=1
3) print 1, $i=0Step 2: Check loop condition after each iteration
Loop continues while $i > 0. After printing 1, $i becomes 0, condition fails, loop stops.Final Answer:
3 2 1 -> Option AQuick Check:
Loop prints 3,2,1 then stops [OK]
- Including 0 in output
- Starting output from 1 instead of 3
- Confusing pre- and post-decrement
do-while loop:$count = 1;
do {
echo $count;
$count++
} while ($count <= 3);Solution
Step 1: Check each line for syntax errors
The line$count++is missing a semicolon at the end, which is required in PHP.Step 2: Verify other parts of the loop
The loop condition and variable initialization are correct. The use ofdois proper for a do-while loop.Final Answer:
Missing semicolon after$count++-> Option CQuick Check:
Every statement must end with semicolon [OK]
- Forgetting semicolon after statements
- Misplacing loop condition parentheses
- Confusing do-while with while syntax
do-while loop correctly implements this?do {
$input = trim(fgets(STDIN));
// process input
} ???;Solution
Step 1: Understand the input condition
We want to continue reading while input is not empty. The PHP functionempty()returns true if input is empty.Step 2: Choose the correct condition
Usingwhile (!empty($input));means loop continues while input has some content, stopping when empty.Final Answer:
while (!empty($input)); -> Option DQuick Check:
Loop continues while input is not empty [OK]
- Using equality to empty string incorrectly
- Using condition that stops loop too early
- Confusing empty() with isset()
Ready to prove it?
Pass the test and this pattern moves unknown
