For Loop

0 kristadane33563 December 7, 2020

Loop defines as a block of the statement which is repeatedly executed for a certain number of times or until a particular condition is satisfied. When an identical task is to be performed a number of times, then it is used.

For example, When we have to print the numbers from 1 to 100. We can use it to print the number from 1 to 100.

We have three types of loop.

For Loop

It is useful to execute a statement a number of times. The syntax is

for(counter initialization, test condition, increment or decrement)
{
    statement; or block of loop
}

The flow of execution of the for loop

The interpreter/compiler takes care of the block of the statement which must execute. The different components in it are executed in a directed sequence as shown in the figure.

For Loop Flowchart
For Loop Flowchart
  • At first; the counter initializes to some value.
  • Then the counter variable is tested with the condition.
  • If the test is true, the body of the loop executes. 
  • After finishing the body, the counter variable increments or decrements.
  • And again updated counter variable tests with test conditions.

The same process repeats as long as the condition is true. If the result with the test condition is false, the control passes outside the loop i.e. the statement following the loop. See the above figure to understand the flow of for loop.

Let’s look upon one example to make a clear concept.

<?php

   for($i=1; $i<=10; $i++){
      // This statement would be executed repeatedly until the condition  i<=10 returns false. 
      echo "The value of i = $i\n";
   }

?>

The output of the above program is:

The value of i = 1
The value of i = 2
The value of i = 3
The value of i = 4
The value of i = 5
The value of i = 6
The value of i = 7
The value of i = 8
The value of i = 9
The value of i = 10

Infinite for loop:

Infinite for loop means the loop which executes repeatedly and never ends. This is usually happening due to mistakes in the program. When you set the condition that doesn’t return false, then in that condition, we can say infinite loop. 

<?php

   for($i=1; $i>=1; $i++){
      echo "The value of i = $i\n";
   }

?>

This is an infinite loop. In this program, the condition is $i >= 1 which is always true for this expression.

Another example of infinite for loop is:

for ( ; ; )
{
    //block of code
}

Foreach Loop

It uses to iterate over arrays. Foreach is only working on PHP arrays and objects. If you try to use foreach loop on the variable then you will get the error. We have two syntaxes to use the foreach loop:

foreach($array as $value){
    //statement - x
}

foreach($array as $key => $value){
    //statement - x
}

On the first syntax, the value of each element assigns to $value. The same thing also happens in the second syntax. The only difference is that In the second each element index assigns to variable $key.

Lets take an example to make clear concepts on foreach loop.

Example 1 : First Syntax

<?php

$array = array("Apple", "Pear", "Banana");
foreach($array as $value){
    echo "Element is $value.\n";
}

?>

The output of above program is

Element is Apple.
Element is Pear.
Element is Banana.

Example 2 : Second Syntax

<?php

$array = array("Apple", "Pear", "Banana");
foreach($array as $key => $value){
    echo "$key Element is $value.\n";
}

?>

The output of above program is

0 Element is Apple.
1 Element is Pear.
2 Element is Banana.