What is Loop What are the Syntax?

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.

Syntax:-

while(condition){
// Code to be executed
}

EX:-

<?php    
for($n=1;$n<=10;$n++){
echo "$n<br/>";
}
?>

Result:

1
2
3
4
5
6
7
8
9
10

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.

do
{
// the body of the loop
}
while (testExpression);

Exmple: –

<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>

Outputs:-

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
Amit Kumar
Tagged : / / /

Leave a Reply