PHP/Statement/do while

Материал из Web эксперт
Перейти к: навигация, поиск

Break in Nested Loops

   <source lang="html4strict">

<html> <head> <title>Break Nested</title> </head> <body> <?php

    srand((double)microtime() * 1000000);
  
    $number = rand(1, 1000);
    $j = 0;
    $outer_loop_itr = 50;
  
    for($i = 1; $i <= $outer_loop_itr; $i++) {
         $j = 0;
         do {
              $j++;
              $number = rand(1, 1000);
              if($number == 999) {
                break;
              }  
         } while(1);
  
         $num_iterations[$i] = $j;
    }
    $avg_iterations = (array_sum($num_iterations) / $outer_loop_itr);
    print("an average of " . $avg_iterations);

?> </body> </html>

      </source>
   
  


Counting to 10 with do ... while

   <source lang="html4strict">

<?php $num = 1; do {

   echo "Number  is ".$num."
"; $num++;

} while ($num <= 10); echo "Done."; ?>

 </source>
   
  


Do while loop

   <source lang="html4strict">
<?php
$a = 1;
do{
   echo $a . "
"; $a++; }while ($a > 10); ?> </source>


do...while loop is executed at least once

   <source lang="html4strict">

<?php

           $i = 11;
           do {
                   print "Number $i\n";
           } while ($i < 10);
   ?>

Same code could be written using a while loop:

   <?php
           $i = 11;
           while ($i < 10) {
                   print "Number $i\n";
           }
   ?>
 
 </source>
   
  


do while loop with counter

   <source lang="html4strict">

<?php $num = 11; do {

   echo $num;
   $num++;

} while ($num <= 10); ?>

 </source>
   
  


do while with integer counter

   <source lang="html4strict">

<?php

   $count = 11;
   do {
       echo "$count squared = ".pow($count,2). "
"; } while ($count < 10);

?>

 </source>