Loop Statement in PHP, for loop in PHP, while loop in PHP, do-while loop in PHP


Definition of Loop:-


Loop is used to print a large range of data using a single repeatable block.  Loop Statement Will work based on three different statements.

1)initialization:-   It is used to provide the starting point of the loop, for example, if we want to print 10 to 50 all data then the initialization point will be 10.

$i=10;

2)condition:-
   It is used to provide limit or the last point of the loop, for example, if we want to print 10 to 50 then the condition will be.


$i<=50;



3)Iteration:-   It is used to increase or decrease the value of the loop.

for example, if we want to display 10 to 50 but we want to increase value by 5 then we can use increment by five.

$i=$i+5;


Type of Loop:-

1)  Exit Control:-

     "First Execute Loop Block Then Check Condition" for example if you first travel into the bus after that pay for the ticket then it is called Exit Control.


do-while:-  It is called Exit Control Loop of PHP 

Syntax:-

init;
do
{
      statement;
      increment;


}while(condition);



Condition decides loop limit until condition false then the loop will execute when condition false then the loop will terminate automatically.



WAP to calculate factorial of any assigned number? 

<?php
$f=1;
$num=5;
$i=$num;
do
{

$f=$f*$i;
$i--;

}while($i>=1);

echo "factorial is ".$f;




?>

WAP to check prime number?


WAP to calculate the sum of one digit positive number?


2) Entry Control:-


   First Check condition then execute Loop Statement hence it is called Entry Control Loop.

    2.1)  While:- 

            init;
           while(condition)
           {
                Statement;
                Increment;
          }


note:  we will prefer while loop when the condition is infinite, for example, if we select data from the database then we use while loop because we will implement a condition that if the row exists then the record will fetch.       

Some assignments of while loop:-


WAP to calculate factorial of any assigned number?


WAP to display Fibonacci series ?

  0 1 1 2 3 5 8 13

 WAP to reverse the five-digit number?

Solution:-
 <?php
$num=12345;
$num1=0;
while($num!=0)
{
   $rem = $num%10; //1
   $num1 = $num1*10+$rem  //54321
   $num=(int)($num/10); //0
}

echo $num1;




?>

  WAP to find the max digit in the mobile number?

<?php
$num=1234567891;

$max=0;

while($num!=0)
{

   $rem = $num%10;
   if($max<$rem)
    $max=$rem;
 
   $num=(int)($num/10);
}

echo $max;




?>

  WAP to calculate the table if the number is even and calculate factorial if the number is odd?

<?php
$num=6;
if($num%2==0)
{
    $i=1;
    while($i<=10)
    {

      $t = $num*$i;
      echo $t,"<br>";
      $i++;
 

     }
}
else
{
    $f=1;
    $i=$num;
    while($i>=1)
    {

      $f=$f*$i;
   
      $i--;
 

     }
     echo $f;

}






?>
 Q) WAP to calculate Square and Cube of prime number from 1 to 50?

<?php

$i=1;
while($i<=50)
{

  $j=1;
   $c=0;
  while($j<=$i)
  {
    if($i%$j==0)
    $c++;

    $j++;

  }
  if($c==2)
  {
     echo "square is $i ",$i*$i,"<br>";
     echo "cube is $i ",$i*$i*$i,"<br>";

  }

  $i++;

}






?>

For loop in PHP:-


It is used to contain all statements using a single for a statement. It is used to solve the finite condition-based program. It provides simple syntax structure as compare to while loop and do--while loop.

for the nested sequence, we should use for loop statement.



<?php

for(init;condition;iteration)
{
     Statement;
}

?>

<?php

      1       2            4
for($i=1;$i<=10;$i++)
{
   3
   echo $i;

}

?>



WAP to check prime number using for loop?

<?php
$num=9;
$c=0;
for($i=1;$i<=$num;$i++)
{
   if($num%$i==0)
   {
       $c++;
   }

}

if($c==2)
{
echo "prime";
}
else
{
echo "not prime";
}
?>


Another way to check the prime number program in PHP?

<?php
$num=9;
$c=0;
for($i=2;$i<$num;$i++)
{
   if($num%$i==0)
   {
       $c++;
       break;
   }

}

if($c==0)
{
echo "prime";
}
else
{
echo "not prime";
}
?>


Prime number program without using the third variable?

<?php
$num=5;
$c=0;
for($i=2;$i<$num;$i++)
{
   if($num%$i==0)
   {
      
       break;
   }

}

if($num==$i)
{
echo "prime";
}
else
{
echo "not prime";
}
?>




Nested For Loop:-  

we can write more than one for loop using nested sequence, it contains a collection of outer for loop and inner for loop. the first outer for loop will execute once, after that inner for loop will execute complete (starting to ending), when inner for loop condition will be false then outer for loop will increase and check condition after that again inner for loop will execute complete when the outer condition will false then program will terminate.



for(init;condition;increment)
{
      for(init;conditon;increment)
      {
              statement;
      }
}

................
...........................................................................

for($i=1;$i<=6;$i++)

{
      for($j=5;$j<=9;$j++)
      {
              echo "@"," ";
      }

     echo "<br>";

}



<?php
for($i=1;$i<=6;$i++)

{
      for($j=5;$j>=$i;$j--)
      {
              echo "@"," ";
      }

     echo "<br>";

}


?>
Output:-

@ @ @ @ @
@ @ @ @
@ @ @
@ @
@
.......................................................................................................................


Assignment for nested for loop in PHP?

................................................................................
1)
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
.....................................................................

2)

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

Solution:-
<?php

for($i=1;$i<=5;$i++)
{
for($j=5;$j>=$i;$j--)
{
echo $j." ";
}
echo "<br>";
}







?>
...............................................................................
3)

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Solution:-
<?php

for($i=5;$i>=1;$i--)
{
for($j=1;$j<=$i;$j++)
{
echo $j." ";
}
echo "<br>";
}







?>
......................................................................................
4)
1 0 0 1 0
1 0 0 1
1 0 0
1 0
1

Solution:-
<?php

for($i=5;$i>=1;$i--)
{
for($j=1;$j<=$i;$j++)
{
if($j==1 || $j==4)
echo "1"." ";
    else
echo "0"." ";
}
echo "<br>";
}







?>
.............................................................................................
5)
A B C D E
A B C D
A B C
A B
A
..............

Solution:-

<?php

for($i=5;$i>=1;$i--)
{
$ch=65;
for($j=1;$j<=$i;$j++)
{
echo chr($ch)." ";
$ch++;
}
echo "<br>";
}







?>..............................................................................


6)

          *
     *    *    *
*   *    *    *  *
     
The solution to this program:-

<?php
for($i=1;$i<=3;$i++)
{
   for($k=2;$k>=$i;$k--)   # k =2 to 3 
   {
         echo "&nbsp;";
   }
   for($j=1;$j<=2*$i-1;$j++) #j= 1 to 5
   {
       echo " *";
   }
   echo "<br>"; 
 
}

?>

7)  

*    *   *    *   *
     *    *    *
           *
*   *    *     *     *
     *    *     *
           *


8)

M A N I S H
     A N I S H
         N I S H
             I S H
                S H
                   H


The solution to this program:-

<?php

$s="MANISH";

for($i=1;$i<=6;$i++)       // 3
{
    
    for($k=1;$k<$i;$k++)  //2
     {
          
          echo "&nbsp;&nbsp;&nbsp;&nbsp;";
         
     }
    for($j=$i-1;$j<6;$j++)  // 2 to 5
     {
         echo $s[$j];
        
     }  
    

    echo "<br>";
}

?>


9)

A a B b C
A a B b
A a B 
A a
A


10)

1
2 3
4 5 6
7 8 9 10


11)

2
3 5
7 11  13
17  19 23  29


The solution to this program:-

<?php
$c=0;
for($num=2;$num<30;$num++)
{
  for($i=2;$i<$num;$i++)
  {
     if($num%$i==0)
     {
        break;
     }
  }
  if($num==$i)
  {
  echo $num." ";
  $c++;
  if($c==1 || $c==3 || $c==6)
  echo "<br>";
  }


}







?>
       

Break Statement in PHP:-

It is used to manually terminate the loop based on condition.

for($i=1;$i<=10;$i++)
{
    echo $i;
    break;

}

break another example:-

<?php

for($i=1;$i<=10;$i++)
{
    
   if($i==3 || $i==5 )
   {
    break;
    }
   echo $i;
}







?>


Another example of a break


<?php

for($i=1;$i<=10;$i++)
{
    
   if($i==3 && $i==5 )
   {
    break;
    }
   echo $i;
}







?>

Continue Statement:-

It is used to skip the data and continue the loop, it's control always will be assigned into Loop block.

Continue never to terminate the loop.


for($i=1;$i<=10;$i++)
{
    echo $i;
   if($i==3)
    continue;

}




Another example of continue:-

<?php

for($i=1;$i<=10;$i++)
{
    
   if($i>=3 && $i<=5 )
   {
    continue;
    }
   echo $i;
}







?>

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post