Looping Statements, while, do…while, for, foreach, continue, break and goto Statements

57 views

Looping Statements

Loops have as purpose to repeat a statement a certain number of times or while a condition is fulfilled.

 

The while Statement

 

The while statement specifies a condition that must be met before execution of its embedded code is terminated. Its syntax is the following:

 

                                    while (expression) {

                                                                statements

                                                          }

 

In the following example, $count is initialized to the value 1. The value of $count is then squared and output. The $count variable is then incremented by 1, and the loop is repeated until the value of $count reaches 5.

 

                                        <?php

                                              $count = 1;

                                              while ($count < 5) {

                                              printf(”%d squared = %d <br />”, $count, pow($count, 2));

                                                $count++;

                                            }

                                          ?>

 

                The output looks like this:

                         1 squared = 1

                          2 squared = 4

                          3 squared = 9

                          4 squared = 16

 

The do…while Statement

 

The do…while looping statement is a variant of while but it verifies the loop conditional at the conclusion of the block rather than at the beginning. The following is its syntax:

 

                                      do {

                                             statements

                                           } while (expression);

 

Consider the following example:

 

                                    <?php

                                               $count = 11;

                                                do {

                                                  printf(”%d squared = %d <br />”, $count, pow($count, 2));

                                                      } while ($count < 10);

                                         ?>

 

        The following is the outcome:

                            11 squared = 121

 

The for Statement

The for statement offers a somewhat more complex looping mechanism than does while. The following is its syntax:

 

                 for (expression1; expression2; expression3)

                                      {

                                            statements

                                    }

 

Consider the following example:

 

                  for ($kilometers = 1; $kilometers <= 5; $kilometers++)

                          {

                            printf(”%d kilometers = %f miles <br />”, $kilometers, $kilometers*0.62140);

                        }

 

The results for all three examples follow:

                        1 kilometers = 0.6214 miles

                        2 kilometers = 1.2428 miles

                        3 kilometers = 1.8642 miles

                        4 kilometers = 2.4856 miles

                        5 kilometers = 3.107 miles

 

The foreach Statement

The foreach looping construct syntax is adept at looping through arrays, pulling each key/value pair from the array until all items have been retrieved or some other internal conditional has been met.

The following is its syntax:

 

              foreach (array_expr as $value)

                                 {

                                      statement

                           }

 

Consider this example. Suppose you want to output an array of links:

 

                   <?php

                           $links = array(”www.apress.com”,”www.php.net”,”www.apache.org”);

                             echo “<b>Online Resources</b>:<br />”;

                                 foreach($links as $link)

                                         {

                                         echo “<a href=\”http://$link\”>$link</a><br />”;

                                       }

                     ?>

 

This would result in the following:

                 Online Resources:<br />

                     <a href=”http://www.apress.com”>http://www.apress.com</a><br />

                     <a href=”http://www.php.net”>http://www.php.net</a><br />

                     <a href=”http://www.apache.org”> http://www.apache.org </a><br />

 

The break and goto Statements

Encountering a break statement will immediately end execution of a do…while, for, foreach, switch, or while block. For example, the following for loop will terminate if a prime number is pseudo-randomly happened upon:

 

                  <?php

                       $primes = array(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47);

                         for($count = 1; $count++; $count < 1000) {

                         $randomNumber = rand(1,50);

                         if (in_array($randomNumber,$primes)) {

                             break;

                                     } else {

                          printf(”Non-prime number found: %d <br />”, $randomNumber);

                   }

}

?>

Sample output follows:

                  Non-prime number found: 48

                  Non-prime number found: 42

                  Prime number found: 17

 

Through the addition of the goto statement, this feature was extended in PHP 6 to support labels. This means you can suddenly jump to a specific location outside of a looping or conditional construct. An example follows:

 

                     <?php

                              for ($count = 0; $count < 10; $count++)

                                                 {

                                          $randomNumber = rand(1,50);

                                           if ($randomNumber < 10)

                                         goto less;

                                              else

                                 echo “Number greater than 10: $randomNumber<br />”;

                                         }

 

                              less:

                                 echo “Number less than 10: $randomNumber<br />”;

                      ?>

 

It produces the following (your output will vary):

                 Number greater than 10: 22

                 Number greater than 10: 21

                 Number greater than 10: 35

                 Number less than 10: 8

 

The continue Statement

The continue statement causes execution of the current loop iteration to end and commence at the beginning of the next iteration. For example, execution of the following while body will recommence if $usernames[$x] is found to have the value missing:

 

             <?php

                $usernames = array(”grace”,”doris”,”gary”,”nate”,”missing”,”tom”);

                   for ($x=0; $x < count($usernames); $x++)

                             {

                   if ($usernames[$x] == “missing”) continue;

                     printf(”Staff member: %s <br />”, $usernames[$x]);

                             }

                   ?>

 

This results in the following output:

                 Staff member: grace

                 Staff member: doris

                 Staff member: gary

                 Staff member: nate

                 Staff member: tom



« Control Structures, Conditional, if, else, elseif, switch Statement File-Inclusion Statements, include(), require() Statement »
Posted on Wednesday, March 4th, 2009 at 7:07 pm under PHP and MySQL - From Beginning to Professional | RSS 2.0 Feed

Post Comment

You must be logged in to post a comment.



ComputerEducationWorld.com All Rights Reserved © RSS | CBSE | Education Boards Of India | What is My IP?