Definition:
It checks the condition.
If the condition is true, then the loop inside the curly braces executes.
After that, the loop rechecks the condition.
If the condition is false, the loop exits from the curly braces.
Now you will clearly understand the code logic with the help of the flowchart.
While Loop Flowchart :
First step: → You will write the program
|
Second step: → Now you will check the condition (true/false)
in the while statement
If you find the condition "True", then
|
Execute Code
|
Go back and check the condition again
If the condition is "False", then
|
Stop the loop
|
End the program
Syntax:
while (condition) {
//The code is written here.
}
Example:
<?php
$i = 1;
while ($i <= 5) {
echo "$i<br>";
$i++;
}
?>
Output:
1 2 3 4 5
