Intro:More about loops work

来源:互联网 发布:linux 安装zlib devel 编辑:程序博客网 时间:2024/04/29 20:26
Combining Loops
Just as with conditional statements,it;s also possible to nest one loop inside another.To illustrate,consider the next example,which nests one for loop inside another to dynamically generate an HTML table.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title>Combining For Loops</title>
</head>
    <body>
    <?php
    //generate an HTML table
    //3 rows,4 columns
    echo "<table border=/"1/">";
    for($row=1;$row<5;$row++){
        echo "<tr>";
        for($col=1;$col<5;$col++){
        echo "<td>Row $row, Column $col</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
    ?>
    </body>
</html>

This script utilizes two for loops.The outer loop is responsible for generating the table rows,and it runs three times.On each iteration of this outer loop,an inner loop is also triggered;this loop is responsible for generating the cells within each row,and it runs four times.The end result is a table with three rows,each containing four cells.

Interrpting and skipping loops
While on the topic of loops,it's intersting to discuss two PHP statements that allow you to either interrupt a loop or ship a pareicular iteration of a loop.PHP's break statement is aptly named:it allows you to break out of a loop at any point.To illustrate,consider the following loop,which would normally iterate five times but stops after the second iteration due to the break statement:

<?php
$count=0;
//loop 5 times
while($count<=4){
    $count++;
    //when the counter hits 3
    //break out of the loop
    if($count==3){
        break;
    }
    echo "This is iteration #$count <br />";
}
?>
--------------------------------------------------------------------
Unlike break,continue doesn't halt processing of the loop;it simply "jumps one" iteration.To see how this works,consider the following loop,which iterates five times but skips the third iteration due to the intervention of contiue statement:
---------------------------------------------------------------------
<?php
$count=0;
//loop 5 times
while($count<=4){
    $count++;
    //when the counter hits 3
    //break out of the loop
    if($count==3){
        continue;
    }
    echo "This is iteration #$count <br />";
}
?>

Try it:Building a factorial calculator
A simple real-world application that you can try for yourself to better understand how loops work,is a factorial calculator.In case you were snoozing in math class the day they discussed factorials,the factorial of a number n is simply the product of all the numbers between n and 1.So,for example,the factorial of 4 is 4*3*2*1=24.
The factorial calculator you'll build in this section is interactive:it asks the user to enter a number into a Web form,and it then calculates the factorial of that number.Here's the code:
-----------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title>Project:Factorial Calculator</title>
</head>
<body>
    <h2>Project:Factorial Calculator</h2>
    <?php
    //if form not yet submitted
    //display form
    if(!isset($_POST['submit'])){
    ?>
    <form method="post" action="factorial.php">
    Enter a number:<br />
    <input type="text" name="num" size="3" />
    <p />
    <input type="sumbit" name="sumbit" value="sumbit" />
    </form>
    <?php
    //if form sumbitted
    //process form input
    }else{
        //retrieve number from form input
        $num=$_POST['num'];
        //check that number is positve
        if($num<=0){
            echo 'ERROR:Please enter a number greater than 0';
            exit();
        }
        //calculate factorial
        //by multiplying the number by all the numbers between itself and 1
        $factorial=1;
        for($x=$num;$x>=1;$x--){
            $factorial*=$x;
        }
        echo "Factorial of $num is:$factorial";
    }
    ?>
</body>
</html>
------------------------------------------------------------------------
Most of the work here is performed by the for loop.This loop's counter is initialized to the number entered by the user,and the loop then runs backward,decrementing the loop counter by 1 on each iteration.Each time the loop runs,the perviously calculated product is multiplied by the current valus of the loop counter.The end result is the factorial of the input number.
原创粉丝点击