The do...while Loop
The example below will write some output, and then increment the variable $i by one. Then the condition is checked, and the loop continues to run, as long as $i is less than or equal to 7.
$i = 5;
do {
echo "The number is " . $i . "<br/>";
$i++;
} while($i <= 7);
//Output
//The number is 5
//The number is 6
//The number is 7
do {
echo "The number is " . $i . "<br/>";
$i++;
} while($i <= 7);
//Output
//The number is 5
//The number is 6
//The number is 7
Note that in a do while loop, the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition is false the first time.
No comments:
Post a Comment