Saturday, January 18, 2014

FOR LOOP

                        




  i.   For Loop
ករណី For Loopយើងប្រើវា នៅពេលដែលយើងដឹងច្បាស់ថា កូដរបស់យើងនេះ វានិងត្រូវដំណើរការចំនួនប៉ុន្មានដង។
Syntax:
For (init counter; test counter; increament counter)
{
កូដដែលនិងត្រូវដំណើរការ}




N
1
Code
<form method="post" target="_self">
            Summary:<input type="text" name="txts" value="<?php echo $_POST["txts"];?>" /><br />
pow by:<input type="text" name="txtp" value="<?php echo $_POST["txtp"];?>" />
<input type="submit" name="btnshow" value="Show" />
</form>
<?php
            $sum=empty($_POST["txts"])?"":$_POST["txts"];
            $pow=empty($_POST["txtp"])?"":$_POST["txtp"];
            $loop=1;
            $total="";
            for($loop=1;$loop<=$sum;$loop++){
                        if($loop==$sum)
                        echo "$loop<sup>$pow</sup>=";
                        else
                        echo "$loop<sup>$pow</sup>"."+";
                        $total=$total+pow($loop,$pow);
            }
            echo $total;
?>
Result


2
Code
<form method="get" target="_self">
            Number of Rows:<input type="text" name="rows" value=<?php echo empty($_GET["rows"])?"":$_GET["rows"];?>><br />
    Number of Cols:<input type="text" name="cols" value=<?php echo empty($_GET["cols"])?"":$_GET["cols"];?>>
<input type="submit" name="btnshow" value="show" />
</form>
<table border="1">
            <?php
                        $row=empty($_GET["rows"])?"":$_GET["rows"];
                        $col=empty($_GET["cols"])?"":$_GET["cols"];
                        $index="";
                        for($r=1;$r<=$row;$r++){
                                    echo "<tr>";
                                    for($c=1;$c<=$col;$c++){
                                                $index++;
                                                echo "<td>$index</td>";
                                    }
                        }?></table>
Result

0 comments:

Post a Comment