The for loop is frequently used, usually where the loop will be traversed a fixed number of times.
Syntax:
Nested For Loop
For loop inside for loop
Syntax:
While loop
The while loop keeps repeating an action until an associated condition returns false.
Syntax:
Nested While loop
While Loop inside While Loop
Do While Loop
The do while loop is similar to while loop, but the condition is checked after the loop body is executed. This ensures that the loop body is run at least once.
In this tutorial iām going to define what is loop and what are the types with syntax with examples.
What is Loop?
Any Repeated task more than one time that is called Loop. In a loop structure, the loop asks a question. If the answer requires action, it is executed. Using loops in computer programs simplifies rather optimizes the process of coding.
Types of Loops?
While Loop.
Do while Loop
For Loop
While Loop:- While loop is a pre-test loop. It first test a specified conditional expression and as long as the condition true action taken. While loop also known as Entry Control Loop
Syntax with example:-
<?php
$am =1;
while($am<=6; { echo "this is while condition: $am <br />"; $am++; } ?>
OutPut š
2. Do while Loop:- Do while loop is similar to while loop but the condition is checked after the loop. This ensure that the loop body is run at least once.
<?php
$ami = 3;
do{ echo "this is Laravel Amit: $ami <br />"; $ami++;
}while($ami<=6);
?>
OutPut š
3. For Loop :- The for is frequently used usullay where the loop will be traversed a fixed numbers of times. A loop variable is used to control the loop
Syntax + example:-
<?php
for($num=1; $num<=5; $num++) { echo "this is for loop: $num <br />"; }