PHP While Loops

0
4261

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:
    • In the syntax of while loop, while is the keyword.
    • The counter variable is initialized first.
    • Then the condition in the while statement is checked.
    • If the condition evaluates to true, then only the control enters the while block and executes the statements.
    • The counter is incremented or decremented inside the while block only. This statement is important because if the counter is not incremented or decremented, its value will remain static and then the condition will never evaluate to false and we the loop will work infinitely and never come to an end.
    • In while loop the statements initialization, condition check and increment/decrement are separated, they are not at one place like for loop.
    • While loop allows entry in its block only if the condition evaluates to true otherwise no.
    • Let us see an example of while loop.
    • Create a new folder named while_loop in the htdocs folder in xampp folder located in C drive. Now open a new notepad++ document and save it as index.php in this newly created while_loop folder.
    • Write the following code in index.php file.
    • In the above code, we have declared a variable $no with value 1. This works as a counter.
    • The statement

      executes and prints square of nos. from 1 to 5:
    • Then comes the while statement. The condition $no<=5 is checked. As the value of $no is 1, the condition 1<=5 evaluates to true and the control enters the while block.
    • The echo statement prints the square of 1, because $no contains value 1.
    • Then the value of $no is incremented to 2 and now $no contains value 2.
    • Now the control again goes to while statement to check the condition, it evaluates to true since 2 is less than 5.
    • Then again control enters the while block and calculates and prints square of 2 i.e. 4.
    • Then again value of $no is incremented to 3.
    • This process of checking condition, executing the echo statement in the while block and incrementing the value of $no continues till the condition $no<=5 evaluates to true.
    • This will continue up to the value of $no is equal to 5. Once the value of $no becomes 6, the loop will terminate and we will get the following output:
    • while_loop_output
      fig 1

  • Do….while loop:
    • 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:
    • In the syntax of do….while loop, do and while are the keywords.
    • The block of statements starts immediately after the do statement.
    • The statements are executed first and then the increment/decrement is performed.
    • Then at the end condition is checked in the while statement. The while statement is terminated with semi-colon.
    • The statements in do….while loop are executed at least once even though the condition evaluates to false, because condition is checked at the end.
    • Therefore if we want that the statements should be executed at least once compulsorily, we should use do….while loop.
    • Let us see an example:
    • Write the following code in index.php file:
    • In the above example we have declared a variable $no with value 5 and a variable $cnt with value 1.
    • The $no is the variable whose value we want to print and $cnt variable is used to count the number of iterations /loops when the number is printed.
    • The control enters the do…while block without any restriction.
    • After entering the statement
      is executed which prints loop iteration no. = 1 number = 5.
    • The printed sentence indicates that the number is 5 at iteration number 1.
    • Then the value of $no variable is decremented by 1 and the value of $cnt variable is incremented by 1.
    • Now the values of $no and $cnt are 4 and 2 respectively.
    • Now the condition $no>=1 && $cnt<=5 is checked in the while statement and evaluated to true since the value of $no i.e. 4 is greater than 1 and the value of $cnt i.e. 1 is less than 5.
    • Then the control again goes to the echo statement and prints the next statement loop iteration no. = 2 number = 4.
    • Then again values of $no and $cnt variables are decremented and incremented respectively and condition is checked. Again the control goes to echo statement since the condition evaluates to true.
    • This statement execution, increment-decrement and condition check continues till the condition does not evaluate to false.
    • Once the condition evaluates to false the loop terminates.
    • The output is shown below:
    • do_while_loop_output
      fig 2

    • Do….while loop is mostly used in menu driven programs.
  • Nesting of while loop:
    • 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:
    • Let us see nesting of a while loop in another while loop.
    • Write the following code in index.php file:
    • Let us understand the example above.
      1. In the above example we have found out factorials of numbers from 1 to 5.
      2. The variables $f1 and $f2 contains the values 1 and 5 respectively.
      3. The outer while loop has condition $f1<=$f2, this states that the loop executes 5 times i.e. from 1 to 5.
      4. When the condition is evaluated to true, the variables $fact and $no are initialized to 1 and value of $f1 respectively.
      5. 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.
      6. $no is initialized to the value of $f1 because we don’t want the value of $f1 to be changed or damaged.
      7. Now after initialization of both the variables the control reaches to inner while loop.
      8. 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.
      9. Now the control enters the inner while block and executes the statements
      10. 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.
      11. 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.
      12. This time the control comes out of the inner while loop and increments value of $f1 by 1. Now value of $f1 is 2.
      13. Now the control goes to the condition of outer while loop and checks it, the condition evaluates to true since 2<=5.
      14. Then again $fact and $no are initialized to 1 and value of $f1 i.e. 2 respectively.
      15. Then it goes to inner while loop condition and evaluates to true since value of $no i.e. 2 is greater than 1.
      16. Next again the value of $fact is calculated, it comes out to be 2 . Then the value of $no is decremented to 1.
      17. Then again the control checks the inner while loop condition and evaluates to true since 1 is equal to 1.
      18. 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.
      19. This dissatisfies the condition of inner loop and then comes out of it.
      20. Increments value of $f1 and again check the outer loop condition.
      21. This process continues till the outer loop condition does not evaluate to false.
    • Here also outer loop executes just once while the inner loop executes completely.
    • Thus we found out the factorial of every number from 1 to 5.
    • The output is shown below:
    • nested_while_loop_output
      fig 3

Thus we completed the while loop and do…while loop in this PHP while loops tutorial.

LEAVE A REPLY

Please enter your comment!
Please enter your name here