Challenge - 5 Problems
Do-While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple do-while loop
What is the output of this PHP code?
PHP
<?php $i = 1; do { echo $i . ' '; $i++; } while ($i <= 3); ?>
Attempts:
2 left
💡 Hint
Remember, do-while runs the loop body at least once before checking the condition.
✗ Incorrect
The loop starts with i=1, prints it, increments i, then checks if i <= 3. It repeats until i becomes 4, so it prints 1 2 3.
❓ Predict Output
intermediate2:00remaining
Do-while loop with initial false condition
What will this PHP code output?
PHP
<?php $j = 5; do { echo $j . ' '; $j++; } while ($j < 5); ?>
Attempts:
2 left
💡 Hint
Do-while executes the block once before checking the condition.
✗ Incorrect
Even though the condition is false initially, the loop body runs once printing 5, then condition fails and loop stops.
🔧 Debug
advanced2:00remaining
Identify the error in this do-while loop
What error does this PHP code produce?
PHP
<?php $k = 0; do { echo $k . ' '; $k++ } while ($k < 3); ?>
Attempts:
2 left
💡 Hint
Check punctuation carefully inside the loop body.
✗ Incorrect
The line '$k++' is missing a semicolon at the end, causing a syntax error.
❓ Predict Output
advanced2:00remaining
Counting iterations in a do-while loop
How many times will the loop body execute in this PHP code?
PHP
<?php $count = 0; $num = 10; do { $count++; $num += 5; } while ($num < 25); echo $count;
Attempts:
2 left
💡 Hint
Trace the value of $num each iteration and count how many times the loop runs.
✗ Incorrect
Initial $num=10, after 1st iteration 15, 2nd iteration 20, 3rd iteration 25. Loop stops after 3 iterations because condition fails when $num=25.
🧠 Conceptual
expert2:00remaining
Understanding do-while loop execution order
Which statement best describes the execution model of a do-while loop in PHP?
Attempts:
2 left
💡 Hint
Think about what makes do-while different from while loops.
✗ Incorrect
In a do-while loop, the body executes first, then the condition is checked. This ensures the loop runs at least once.