In the last tutorial, we have discussed for a loop. There, we also discussed loops in PHP . On this, we will talk about while loop. Loop is an interactive control structure, which executes the number block of code until the specified number. In the simple words, the loops base on condition until the condition matches, then the code is executed and when the condition does not match then the loop does not work.
Definition and use of the While Loop
While loop depends on the expression if a test expression is true, while statement will execute the block of code. After the code executing the while loop checks the expression again and while loop will continue until the condition (expression ) is false.
In simple words, if the condition is true then execute the code of block and after code executed while statement checks the condition is true or not if the condition is true then the loop will continue and the condition is not true then exit the block of code.
In other words, in while loop the compiler checks the condition first and executes the code. After that, increment for the next loop. If the increment value matches the condition, then the compiler executes the code of block again. The compiler continues to execute the code of block until the condition matches. If the condition does not match then the compiler gets exit from the code of the block.
That is known while loop.
In the given syntax, you will get better understanding about while loop.
code to be executed;
In this syntax, we give an expression with while loop inside the parenthesis. The expression is also called condition. The condition of this while loop gives to the compiler. How many numbers does the compiler have to execute the block of code.
Examples of While Loop
The following example ,We create a table with the help of the while loop.
Make a table
//first check the condition
//after check the condition code will execute .
In this above example, we have created a variable. After Creating Variables, we create a condition inside the while loop, the loop continues, when the condition is true when the condition is false then the loop does not continue and the compiler gets exit from the code of the block.
Pyramid pattern using While Loop
With the help of while Loop, we can also create pyramid patterns.
* * *
* * * *
* * * * *
In the above example, we have created two variables and used while loop inside the while loop to create a Pyramid pattern.
Year of birth using While Loop
Any registration form is required to create the year of birth. Year of birth can be easily displayed with the help of While Loop.
Year of Birth
In the above example, we have displayed the year of birth. With the help of a while loop, you can display the year of birth from the initial year to ending the year. In this example, we have used the decrement operator (--) . The first value decrement and then print. With the while loop, you can easily execute the block of code until the condition is true.
When the condition is false, the compiler gets exit from the block.
Support Us - Subscribe Now
Integrate Payumoney payment gateway in PHP website | Source Scripts
Dynamic Countdown timer using JS ,PHP and MYSQL | Animated timer
Integrate PayPal payment gateway in PHP with MYSQL database | PHP scripts