In this turorial i’m going to learn about Lools, how to use and how to write.
What is loop?
A Loop is an Iterative Control Structure that involves executing the same number of code a whatever you write in the codition its used to execute the same block code again to again whenever its condition is false.
// Code to be executed
When to use while loops
- While loops are used to execute a block of code until a certain condition becomes true.
- You can use a while loop to read records returned from a database query.
Types of while loops
- Do… while — executes the block of code at least once before evaluating the condition
- While… — checks the condition first. If it evaluates to true, the block of code is executed as long as the condition is true. If it evaluates to false, the execution of the while loop is terminated.
The Do while loop is similar to the while loop with one important difference. The body of do while loop is executed at least once. Only then, the test expression is excuted.
// the body of the loop
$x = 1;
echo "The number is: $x <br>";
} while ($x <= 5);
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5