do-while
Loopdo-while
loops are very similar to while
loops.
do-while
expression is checked at the end of each iteration.
So the nested statement(s) would be executed once.
then the condition is evaluated,the nested statement(s) would be executed repeatedly as long as the specified condition evaluates to TRUE.
structure:
do{ //do this code; }while ( conditional expr)
Example for do-while loops:
<?php $i = 1; do { echo "do-while loop ".$i; }while ($i > 1); // compare with while loop while ($i > 1){ echo "while loop ".$i; } ?>
with a while
loop, the condition expression is checked in the beginning of each iteration.
With a do-while
loop, the loop will always be executed once,
Because the condition expression is checked at the end of each iteration.