Repeating statement with iterators or looping statement
These looping statement helps to repeat the statement until condition is satisfied.these looping statement are also called as repeating statement or loop statement, iterators.this looping statement reduces time and it helps to maintain code easy for the user .these are four types of looping statement.those are
1.for loop
2.while loop
3.do while loop
4.foreach loop
for loop:
for loop is used for repeating a statements in many time as much as you needed .here user must know how many times this iteration should perform.for loop consist of three fields.one is initialisation,another one is condition,last one is increment/decrement.initialisation means assigning a initial value to the variable.next step is checking the condition if the condition is true it goes the block of statement otherwise it stops the block of statement.
Syntax for loop:
for(initialisation; condition; increment/decrement){
Statement 1;
Statement 2;
}
Write a program to print 1-20using for loop in php
<?php
for($num=1;$num<=20;$num++)
{
echo “$num”.”\n”;
}
?>
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Explanation:
num ane variable ki value 1 store ayyinddi.tharwatha condition check chestundi, condition true aithe for loop block lo unna number print avuthundi.aa tharwatha num anedhi increment ayyi 1 add ayyiddi.a tharwatha 2<=20ane condition ni check chesi condition true ayyinddi kanaka 2print avuthundi ala continue ga check chestu until 20<=20 anedi check chesi 20print avuthundi.21ayyaka condition false avuthundi kanukuna program end avuthundi.
While loop :
While loop is only known as entry control loop.theseare used to check condition and execute the block of statement.according condition the program is executed for that numbers times repeatedly.this is also work like for loop but syntax and representation is different.we cannot write while loop in single line.
Syntax for while loop:
initialisation;
while(condition)
{
//Statement 1;
//statement 2;
//Statement 3;
Increment/decrement;
}
Program using while loop:
We are writing a program using while loop to
print tables.
<?php
$n=1;
while($n<=10)
{
echo “10 X ” .$n ” = ” .(10 *$n).”<br>”;
$n++;
}
?>
Output 1:
10 X 1 =10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9= 90
10 X 10= 100
Explanation:
Paina unna program lo first $n ane variable ki 1 value assign ayyiddi. A tharwatha condition check chestundi n anedi less than are equal to 10 anedi check chesi condition true aithe loop loki velthundi.akkada nenu 10th table display avvali anukuntunnanu kanuka 10 X n =10*n ani print chestundi ala continue ayyi 10 varaku vastundi .a next condition false avuthundi kanukuna program exit avuthundi.
do while loop:
do while loop is a exit loop.it is works like a while loop.but the difference is in do while program is executed first then it checks the condition.difference between while loop and do while loop is do while execute unnecessary one time execution without checking condition.even it prints one time output condition fails.that makes storage wastage.
Syntax for do while:
do
{
//Statement 1;
//Statement 2;
Increment/decrement;
}
While(condition);
Program for do while:
<?php
$num=1;
do
{
echo $num.”\n”;
}
While($num<20);
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Explanation:
do while loop lo first do rastamu dani Lopala code rasi, increment/decrement chesi program end lo while lo condition rastamu.do while lo condition check cheyakundane program run ayyi output print avuthundi.aa tharwatha while lo condition check chesi adhi false ayyaka program exit avuthundi.
foreach loop in php:
foreach loop is one of the important loop in php.it is used for arrays.it takes one after another element from the array.it takes element variable without any index.
Syntax for foreach loop:
Foreach($array as $value)
{
//Statement 1;
//Statement 2;
}
Program for foreach loop:
<?php
$fruits=array(“apple”,”grapes”,”mango”,”pomegranate”,”pears”);
foreach($fruits as $fruitnames)
{
echo $fruitnames.”\n”;
}
?>
Output:
apple
grapes
mango
pomegranate
Pears.
Explanation:
foreach loop program lo arrays lo element ni okko element ni read cheyataniki use avuthundi.fruits as values undatam valana okko array element display avuthundi.aa values anni ayipoyaka program exit avuthundi.