do/while
loop
$rolls = 1;
do {
$rolls++;
} while (mt_rand(1,6) != 6); // get a random number from 1 to 6
echo("<p>Number of times to roll a six: $rolls.</p>\n");
Number of times to roll a six: 18.
do-while
loop to find the max and min of 10 random values
$max = -INF;
$min = INF;
$count = 0;
do {
$number = mt_rand(0, 100);
$max = max($max, $number);
$min = min($min, $number); $count++;
}
while ($count < 10);
echo("<p>Max: $max Min: $min</p>\n");
Max: 99 Min: 5
do-while
statement executes the block of statements within its braces as long as its conditional expression is TRUE.do-while
statement, the condition is tested at the end of the do-while
loop. This means the code is always executed at least once.mt_rand()
and max() functions, which are presented in the chapter 9. The mt_rand()
function gets a random number in the range specified by its arguments. The max()
function gets the larger of the two values that are passed to it as arguments.