Skipping Interactions with continue


Skipping Interactions with continue

Here's another tip: you can use the continue statement inside a loop to skip the rest of the current loop iteration and continue with the next iteration. That's useful in case you need to skip an iteration, as in phpcontinue.php, Example 2-10, which prints out reciprocals (one over a number, 1/n). In this case, we use continue to avoid trying to figure out 1/0 (which is infinite and would cause a math error).

Example 2-10. Using the continue statement, phpcontinue.php
 <HTML>     <HEAD><TITLE>Using the continue statement</TITLE></HEAD>     <BODY>         <H1>Using the continue statement</H1>         <?php             for ($value = -2; $value < 2; $value++){                 if($value == 0){                     continue;                 }                 echo "1/$value = ", 1 / $value, "<BR>";             }         ?>     </BODY> </HTML> 

The results appear in Figure 2-11. Note that we skipped the problematic iteration!

Figure 2-11. Using the continue statement.




    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net