التخطي إلى المحتوى الرئيسي

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


تعليقات

  1. MANSI DUBEY


    =1)
    {

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

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

    ردحذف
  2. Kritika Barod

    program to check prime number

    ردحذف
  3. MANSI DUBEY


    ";

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

    ردحذف
  4. Kritika Barod

    =1);

    echo "factorial is ".$f;
    ?>

    ردحذف
  5. load->view('primeview',array("num"=>$num,"output"=>$res));

    }


    }




    ?>

    ردحذف
  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));

    }


    }

    ردحذف
  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";

    ردحذف
  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 ";

    ردحذف
  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;
    }

    ردحذف
  10. REVERSE NUMBER
    Shubham Nandwal

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

    ردحذف
  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 ";

    ردحذف
  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";
    }

    ردحذف

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

    ردحذف

  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;


    }
    ?>

    ردحذف

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

    ردحذف
  16. mohammad kabir












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

    ردحذف
  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";
    }

    ردحذف

  18. Assignment for nested for loop in PHP?

    ";
    }


    ?>

    output:
    12345
    12345
    12345
    12345

    ishu manglam

    ردحذف

إرسال تعليق

POST Answer of Questions and ASK to Doubt

المشاركات الشائعة من هذه المدونة

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...