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.
Definition of Loop:-
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);
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
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)
6)
*
* * *
* * * * *
The solution to this program:-
<?php
for($i=1;$i<=3;$i++)
{
for($k=2;$k>=$i;$k--) # k =2 to 3
{
echo " ";
}
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 " ";
}
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;
}
?>
MANSI DUBEY
ReplyDelete";
}
?>
MANSI DUBEY
ReplyDelete=1)
{
$f=$f*$i;
$i--;
}
echo "factorial is ".$f;
?>
Kritika Barod
ReplyDeleteprogram to check prime number
MANSI DUBEY
Delete";
}
?>
MANSI DUBEY
ReplyDelete";
echo $t2."
";
for ($i = 1; $i <= 6; $i++)
{
$f3 = $t1 + $t2;
$t1 = $t2;
$t2 = $f3;
echo $f3 ."
";
}
?>
MANSI DUBEY
ReplyDeleteKritika Barod
ReplyDelete=1);
echo "factorial is ".$f;
?>
Kritika Barod
ReplyDeleteKritika Barod
ReplyDeleteload->view('primeview',array("num"=>$num,"output"=>$res));
ReplyDelete}
}
?>
class Prime extends CI_Controller
ReplyDelete{
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));
}
}
SUM OF ONE DIGIT +VE NUMBER
ReplyDeleteSHUBHAM NANDWAL
$t=0;
for($i=1;$i<=9;$i++)
{
$t=$i+$t;
}
echo" SUM OF ONE DIGIT POSITIVE NUMBER IS = $t";
Find Factorial Using While loop
ReplyDeleteShubham Nandwal
$n=9;
$f=1;
$i=$n;
while($i>=1)
{
$f=$f*$i;
$i--;
}
echo " FACTORIAL OF $n IS = $f ";
Fibonacci Series
ReplyDeleteShubham Nandwal
$n=0;
$n1=1;
$n2=0;
for($i=1;$i<20;$i++)
{
echo "$n","
";
$n=$n1+$n2;
$n1=$n2;
$n2=$n;
}
REVERSE NUMBER
ReplyDeleteShubham Nandwal
$n=789654456;
while($n!=0)
{
$rev=$n%10;
$n= (int)($n/10);
echo "$rev";
}
MAX DIGIT IN MOBILE NUMBER:
ReplyDeleteShubham 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 ";
IF NUM IS EVEN >>TABLE, IF NUM IS ODD>>FACTORIAL
ReplyDeleteshubham 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=1)
{
$fact=$fact*$num;
$num=$num-1;
}
echo "Factorial of given number is :"."$fact";
echo"
";
ReplyDelete// 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// 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 "
";
}
mohammad kabir
ReplyDeleteelse
{ if ($c>$d)
{
echo "$c is greater";
}
else
{
echo "$d is greater";
}
}
mohammad kabir
ReplyDelete$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";
}
ReplyDeleteAssignment for nested for loop in PHP?
";
}
?>
output:
12345
12345
12345
12345
ishu manglam