Wednesday, October 24, 2012

PHP Do While Tutorial with Working Example

On reaching the control to do statement, the program proceeds to evaluated the body of the loop first. At the end of the loop, the test-condition in the while statement is evaluated,so it is categorized as post tested loop.
The basic format of do while() statement is:
do{
      body of the loop
     }while(test-conditon);

Lets have a working example
a program to enter a number and check entered number is Armstrong or Not (153 is a Armstrong number).                
<?php  
if(isset($_POST['b1'])) {
                                $number=$_POST['t1'];
                                $cp=$number;
                                $digit=0;
                                $cube_plus=0;
                                do{
                                   $digit=$number%10;
                                   $cube_plus=$cube_plus+($digit*$digit*$digit);
                                   $number=(int)($number/10);
                                  }while($number>0);
                                 if($cube_plus==$cp)
                                 $msg="Armstrong";
                                 else
                                 $msg="Not Armstrong";
                }
                ?>
                <form action="" method="post">
                Enter Number<input type="text" name="t1"><br>
                Status<input type="text" value="<?php echo $msg;?>"><br>
                <input type="submit" name="b1" value="Check">
                </form>
 

PickMe
Delicious Save this on Delicious

No comments :

Post a Comment

If you have any Question , Suggestion or idea then please comment regarding the post.