Concept Flow - Do-while loop execution model
Start
Execute body
Check condition
Yes| No
Repeat
The do-while loop runs the code inside the loop first, then checks the condition. If true, it repeats; if false, it stops.
<?php $i = 1; do { echo $i . " "; $i++; } while ($i <= 3); ?>
| Step | Variable i | Condition ($i <= 3) | Action | Output |
|---|---|---|---|---|
| 1 | 1 | 1 <= 3 is True | Print 1, increment i to 2 | 1 |
| 2 | 2 | 2 <= 3 is True | Print 2, increment i to 3 | 2 |
| 3 | 3 | 3 <= 3 is True | Print 3, increment i to 4 | 3 |
| 4 | 4 | 4 <= 3 is False | Exit loop |
| Variable | Start | After 1 | After 2 | After 3 | Final |
|---|---|---|---|---|---|
| i | 1 | 2 | 3 | 4 | 4 |
do-while loop syntax in PHP:
do {
// code to run
} while (condition);
Runs loop body first, then checks condition.
Repeats if condition true, stops if false.
Always runs at least once.do-while loop and a while loop in PHP?do-while loopdo-while loop runs the code block first, then checks the condition to decide if it should repeat.while loop behaviorwhile loop checks the condition first before running the code block, so it may not run at all if the condition is false initially.do-while loop executes the code block at least once before checking the condition. -> Option Bdo-while runs code once before condition check [OK]do-while loop in PHP?do-while syntaxdo { /* code */ } while (condition); with a semicolon after the while condition.do-while syntax ends with semicolon [OK]$i = 3;
do {
echo $i . ' ';
$i--;
} while ($i > 0);do-while loop:$count = 1;
do {
echo $count;
$count++
} while ($count <= 3);$count++ is missing a semicolon at the end, which is required in PHP.do is proper for a do-while loop.$count++ -> Option Cdo-while loop correctly implements this?do {
$input = trim(fgets(STDIN));
// process input
} ???;empty() returns true if input is empty.while (!empty($input)); means loop continues while input has some content, stopping when empty.Pass the test and this pattern moves unknown