Do While loop works same as other programming languages. In the Do while loop, The only difference is that a do while loop checks its truth expression before ending each iteration. Basically, this kind of loop makes sure that your statement will run at least once, regardless of the truth expression’s value.
Syntax:-
do
statement
while
(expression);
Example:-
<?php
$x=1;
do{
print $x.'<br/>';
$x++;
} while($x<5)
?>
2
3
4
Php do while loop – Interview Questions
Q 1: What is do-while loop?
Ans: Executes code at least once before checking condition.
Q 2: When is the condition checked?
Ans: After executing loop body.
Q 3: Syntax example?
Ans: do { } while ($i < 5);
Q 4: Difference between while and do-while?
Ans: do-while runs at least once.
Q 5: When to use do-while?
Ans: When execution is required at least once.
Php do while loop – Objective Questions (MCQs)
Q1. do...while loop executes the block ______.
Q2. Condition in do...while is checked ______ the loop body.
Q3. do...while ends with ______.
Q4. Which loop guarantees at least one execution?
Q5. do...while is useful when the loop body must run ______.