Recall & Review
beginner
What is a do-while loop in PHP?
A do-while loop is a control structure that executes a block of code once, and then repeats the loop as long as a specified condition is true.
Click to reveal answer
beginner
How does the execution order of a do-while loop differ from a while loop?
A do-while loop executes the code block first, then checks the condition. A while loop checks the condition first before executing the code block.
Click to reveal answer
beginner
What will happen if the condition in a do-while loop is false on the first check?
The code block will still run once before the condition is checked and the loop stops.
Click to reveal answer
beginner
Write the basic syntax of a do-while loop in PHP.
do {
// code to execute
} while (condition);
Click to reveal answer
beginner
Why might you choose a do-while loop over a while loop?
You use a do-while loop when you want the code inside the loop to run at least once, regardless of the condition.
Click to reveal answer
In a do-while loop, when is the condition checked?
✗ Incorrect
The do-while loop always executes the loop body first, then checks the condition.
What will this PHP code output?
$i = 5;
do {
echo $i . ' ';
$i++;
} while ($i < 5);
✗ Incorrect
The loop runs once printing 5, then checks condition ($i < 5) which is false, so it stops.
Which of these is true about do-while loops?
✗ Incorrect
Do-while loops execute the loop body first, so they always run at least once.
What keyword starts a do-while loop in PHP?
✗ Incorrect
The do-while loop starts with the keyword 'do'.
If you want to ensure a block of code runs at least once, which loop should you use?
✗ Incorrect
The do-while loop guarantees the code runs once before checking the condition.
Explain how the do-while loop executes in PHP and why it might be useful.
Think about when you want code to run before checking a condition.
You got /4 concepts.
Write a simple PHP do-while loop that prints numbers from 1 to 3.
Start with a variable at 1 and increase it inside the loop.
You got /4 concepts.