Decision and Looping On PHP

6:04 AM | , , , , , ,

This is a simple php script about decision and looping. This script is showing some series number with differrent form and a blok of multiplication table.

<?php
            //This for the title
            echo"<h1>Tugas Pemrograman Web</br></h1>
                         <h2> NIM  : 095410170 </br>
                 Nama  : Alir Retno </br> </h2>
                 1. a)&nbsp;&nbsp;&nbsp;";

            //Ini utk deret ke-1
            $bil=4;
            for($i=2;$i<=7;$i++){
                        echo " $bil , ";
                        $bil+=$i;
            }
            echo "$bil</br>";
            echo "&nbsp;&nbsp;&nbsp; b)&nbsp;&nbsp;&nbsp;";
           
            //Ini utk deret ke-2
            $i=2;
            while($i<=4){
                        echo " ".$i." , ".$i++." , ";
            }
            echo "$i</br>";        
            echo "&nbsp;&nbsp;&nbsp; c)&nbsp;&nbsp;&nbsp;";
           
            //Ini utk deret ke-3
            $bil=1;
            $i=1;
            do{
                        echo " $bil , ";
                        if($i++%2==0) $bil-=7;
                        else $bil+=8;
            }while($i<=6);
            echo "$bil</br></br>           
2. Tabel Perkalian 1 s.d 10</br>";
           
            //ini utk tabel perkalian
            //Bikin tabelnya
            echo "<table border='1'>";
                        for($x=0;$x<=10;$x++){
                                    echo "<tr>";
                                    for($y=0;$y<=10;$y++){
                                                if($x==0 and $y==0){
                                                            echo "<td bgcolor='yellow' align='center'>X</td>";
                                                }else if($x==0 and $y>0){
                                                            echo "<td bgcolor='orange' align='right'> $y </td>";
                                                }else if($x>0 and $y==0){
                                                            echo "<td bgcolor='orange' align='right'> $x </td>";
                                                }else if($x%2==0){
                                                            echo "<td bgcolor='cyan' align='right'> ".$x*$y." </td>";
                                                }else{
                                                            echo "<td bgcolor='pink' align='right'> ".$x*$y." </td>";
                                                }                                             
                                    }         
                                    echo "</tr>";               
                        }
            echo "</table>"; 
?>



 Okay, lets me to tell you about the script one by one. For the first series of number number 4,6,9,13,18,24,31. It's a series number with (i+1) deviation every an iteration. The difference between the first number and the second is two. And then the second and next number difference is three, and so on. So we need a loop with this code
            $bil=4;
            for($i=2;$i<=7;$i++){
                        echo " $bil , ";
                        $bil+=$i;
            }
            echo "$bil</br>";
$i is iteration that you will run and $bil is the number which you will show .


For the second series of number are 2,2,3,3,4,4,5. It's a series number that the number is repeated twice,
so if you wan to show them you can use this code
            $i=2;
            while($i<=4){
                        echo " ".$i." , ".$i++." , ";
            }
            echo "$i</br>";        
 it's so simple guys.... :)


And the last series of number are 1,9,2,10,3,11,4. For this series, the difference between the first number and the second one is plus eight, but  the difference between the second and the third is minus seven. so let's us to do with this code
            $bil=1;
            $i=1;
            do{
                        echo " $bil , ";
                        if($i++%2==0) $bil-=7;

                        else $bil+=8;
            }while($i<=6);
            echo "$bil</br></br>            

The line with red color is the key of code. If an iteration is even then the number minus seven, and if it's an odd iteration then the number minus seven.

If you want to try this code you can search on My Free Download Pages with this title. Good job guys, we will see again soon :)

0 comments:

Post a Comment

Please leave a comment