Skip to main content

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


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;
}
}


Comments

  1. MANSI DUBEY


    =1)
    {

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

    }
    echo "factorial is ".$f;
    ?>

    ReplyDelete
  2. Kritika Barod

    program to check prime number

    ReplyDelete
  3. MANSI DUBEY


    ";

    echo $t2."
    ";
    for ($i = 1; $i <= 6; $i++)
    {
    $f3 = $t1 + $t2;
    $t1 = $t2;
    $t2 = $f3;
    echo $f3 ."
    ";
    }
    ?>

    ReplyDelete
  4. Kritika Barod

    =1);

    echo "factorial is ".$f;
    ?>

    ReplyDelete
  5. load->view('primeview',array("num"=>$num,"output"=>$res));

    }


    }




    ?>

    ReplyDelete
  6. class Prime extends CI_Controller
    {
    function index()
    {
    $num=9;
    $res='';
    for($i=2;$i<$num;$i++)
    {
    if($num%$i==0)
    {
    $res='NOT Prime';
    break;
    }

    }

    if($num==$i)
    {
    $res = "Prime";
    }

    $this->load->view('primeview',array("num"=>$num,"output"=>$res));

    }


    }

    ReplyDelete
  7. SUM OF ONE DIGIT +VE NUMBER
    SHUBHAM NANDWAL
    $t=0;
    for($i=1;$i<=9;$i++)
    {
    $t=$i+$t;
    }
    echo" SUM OF ONE DIGIT POSITIVE NUMBER IS = $t";

    ReplyDelete
  8. Find Factorial Using While loop
    Shubham Nandwal

    $n=9;
    $f=1;
    $i=$n;

    while($i>=1)
    {
    $f=$f*$i;
    $i--;
    }

    echo " FACTORIAL OF $n IS = $f ";

    ReplyDelete
  9. Fibonacci Series
    Shubham Nandwal

    $n=0;
    $n1=1;
    $n2=0;

    for($i=1;$i<20;$i++)
    {
    echo "$n","
    ";
    $n=$n1+$n2;
    $n1=$n2;
    $n2=$n;
    }

    ReplyDelete
  10. REVERSE NUMBER
    Shubham Nandwal

    $n=789654456;
    while($n!=0)
    {
    $rev=$n%10;
    $n= (int)($n/10);
    echo "$rev";
    }

    ReplyDelete
  11. MAX DIGIT IN MOBILE NUMBER:
    Shubham Nandwal

    $mn=8874578764;
    $max=0;
    while($mn!=0)
    {
    $rem=$mn%10;

    if($max<$rem)
    {
    $max=$rem;
    }
    $mn= (int)($mn/10);
    }
    echo"MAX DIGIT IN THIS MOBILE NUMBER IS $max ";

    ReplyDelete
  12. IF NUM IS EVEN >>TABLE, IF NUM IS ODD>>FACTORIAL
    shubham Nandwal

    $n=5;
    $f=1;
    $t=0;
    if($n%2==0)
    {
    echo"TABLE OF EVEN NUMBER $n IS FOLLOWING","
    ","
    ";
    for($i=1;$i<=10;$i++)
    {
    $t=$i*$n;

    echo"$t","
    ";
    }
    }
    else
    {
    echo"FACTORIAL OF ODD NUMBER $n IS FOLLOWING","
    ","
    ";
    for($i=$n;$i>0;$i--)
    {
    $f=$f*$i;
    }

    echo "$f";
    }

    ReplyDelete

  13. =1)
    {
    $fact=$fact*$num;
    $num=$num-1;
    }
    echo "Factorial of given number is :"."$fact";
    echo"
    ";

    ReplyDelete

  14. // WAP to display Fibonacci series

    $a=0;
    $b=1;
    echo "Fibonacci series:";
    echo "$a";
    echo " $b";
    $c=0;
    while($c<=13)
    {
    $c=$a+$b;
    echo " $c";
    $a=$b;
    $b=$c;


    }
    ?>

    ReplyDelete

  15. // 1 2 3 4 5
    // 1 2 3 4 5
    // 1 2 3 4 5
    // 1 2 3 4 5
    // 1 2 3 4 5

    for($i=1;$i<6;$i++)
    {
    for($j=1;$j<6;$j++)
    {
    echo " $j";
    }
    echo "
    ";
    }

    ReplyDelete
  16. mohammad kabir












    else
    { if ($c>$d)
    {
    echo "$c is greater";
    }
    else
    {
    echo "$d is greater";
    }
    }

    ReplyDelete
  17. mohammad kabir









    $a=676;
    $pri=0;
    for ($i=1;$i<=$a;$i++)
    {
    if ($a%$i==0)
    {
    $pri++;
    }
    }
    if ($pri==2)
    {
    echo "$a is prime no";
    }
    else
    {
    echo "$a is not prime no";
    }

    ReplyDelete

  18. Assignment for nested for loop in PHP?

    ";
    }


    ?>

    output:
    12345
    12345
    12345
    12345

    ishu manglam

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...