We now know that loops are used to repeat the execution of the statements given number of times instead of writing the statements that number of times. This helps us to reduce the length of program also. We studied for loop and foreach loop in the last tutorial, today we will learn the remaining while loop and do while loop.
Let us start.
- While loop:
- A while loop repeats the statements given in its block as long as the condition is true.
- Syntax of while loop is given below:
Initialization of counter; while(condition) { Statement(s); //code to be executed Counter increment/decrement; }
<html> <head> <title>PHP while loops</title> </head> <body> <h3>demonstration of while loop</h3> <?php $no=1; echo "<strong>square of nos. from 1 to 5:</strong><br><br>"; while($no<=5) { echo "<strong> ".($no*$no)."</strong><br>"; $no++; } ?> </body> </html>
echo "<strong>square of nos. from 1 to 5:</strong><br><br>";
executes and prints square of nos. from 1 to 5:
- Do….while loop is another loop same like while loop.
- The only difference between both is that in while loop the condition is checked at the beginning and in do….while loop the condition is checked at the end.
- Syntax of do….while loop is as follows:
do { Statement(s); //code to be executed Increment/decrement; }while(condition);
<html> <head> <title>PHP while loops</title> </head> <body> <h3>demonstration of do....while loop</h3> <?php $no=5; $cnt=1; do { echo "<strong>loop iteration no. = $cnt number = ".$no."</strong><br>"; $no--; $cnt++; }while($no>=1 && $cnt<=5); ?> </body> </html>
echo "<strong>loop iteration no. = $cnt number = ".$no."</strong><br>";
is executed which prints loop iteration no. = 1 number = 5.
- We can use one while loop inside another. You can do nesting of any loops that is, a while loop in another while loop, a for loop in another for loop, a while loop inside for loop or a for loop inside a while loop etc.
- Syntax of nested while loop is as follows:
while(condition) { Statement(s); //code to be executed while(condition) { Statement(s); //code to be executed } }
<html> <head> <title>PHP while loops</title> </head> <body> <h3>demonstration of nested while loop</h3> <?php //demonstration of nested while loop $f1=1; $f2=5; while($f1<=$f2) { $fact=1; $no=$f1; while($no>=1) { $fact=$fact*$no; $no--; } echo "<strong>Factorial of $f1 = ".$fact."</strong><br>"; $f1++; } ?> </body> </html>
- In the above example we have found out factorials of numbers from 1 to 5.
- The variables $f1 and $f2 contains the values 1 and 5 respectively.
- The outer while loop has condition $f1<=$f2, this states that the loop executes 5 times i.e. from 1 to 5.
- When the condition is evaluated to true, the variables $fact and $no are initialized to 1 and value of $f1 respectively.
- We have initialized $fact to 1 because we want to multiply it with a no. and store the result in it. If we initialize it to zero, anything multiplied to it will give zero result.
- $no is initialized to the value of $f1 because we don’t want the value of $f1 to be changed or damaged.
- Now after initialization of both the variables the control reaches to inner while loop.
- It checks the condition in inner while loop that is $no>=1. This condition evaluates to true since $no has value 1 and 1 is equal to 1 according to the condition.
- Now the control enters the inner while block and executes the statements
$fact=$fact*$no; $no--;
- The calculation is done and stored in $fact variable and then the value of $no is decremented. Now the $fact has value 1 in it which is the factorial of 1 and $no has value zero in it.
- The control again goes to the inner while loop condition. But this time the condition evaluates to false because value of $no i.e. 0 is neither greater than nor equal to 1.
- This time the control comes out of the inner while loop and increments value of $f1 by 1. Now value of $f1 is 2.
- Now the control goes to the condition of outer while loop and checks it, the condition evaluates to true since 2<=5.
- Then again $fact and $no are initialized to 1 and value of $f1 i.e. 2 respectively.
- Then it goes to inner while loop condition and evaluates to true since value of $no i.e. 2 is greater than 1.
- Next again the value of $fact is calculated, it comes out to be 2 . Then the value of $no is decremented to 1.
- Then again the control checks the inner while loop condition and evaluates to true since 1 is equal to 1.
- Then the value of $fact is calculated and it comes out to be 2 which is the factorial of 2. Then the value of $n is decremented by 1 which comes out to be zero.
- This dissatisfies the condition of inner loop and then comes out of it.
- Increments value of $f1 and again check the outer loop condition.
- This process continues till the outer loop condition does not evaluate to false.
Thus we completed the while loop and do…while loop in this PHP while loops tutorial.