0
0
PHPprogramming~20 mins

Do-while loop execution model in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Do-While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
?>
ANo output
B1 2
C1 2 3
D2 3
Attempts:
2 left
💡 Hint
Remember, do-while runs the loop body at least once before checking the condition.
Predict Output
intermediate
2: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);
?>
A5
BNo output
C5 6
DSyntax error
Attempts:
2 left
💡 Hint
Do-while executes the block once before checking the condition.
🔧 Debug
advanced
2: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);
?>
ASyntaxError: missing semicolon
BRuntimeError: infinite loop
CNo error, outputs '0 1 2 '
DTypeError: invalid condition
Attempts:
2 left
💡 Hint
Check punctuation carefully inside the loop body.
Predict Output
advanced
2: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;
A5
B2
C4
D3
Attempts:
2 left
💡 Hint
Trace the value of $num each iteration and count how many times the loop runs.
🧠 Conceptual
expert
2:00remaining
Understanding do-while loop execution order
Which statement best describes the execution model of a do-while loop in PHP?
AThe condition is checked before the loop body runs, so the body may not run at all.
BThe loop body runs once before the condition is checked, guaranteeing at least one execution.
CThe loop body and condition are evaluated simultaneously, so execution depends on both.
DThe loop body runs only if the condition is true at the start and after each iteration.
Attempts:
2 left
💡 Hint
Think about what makes do-while different from while loops.