LECTURE 5-Flow Control-Looping
LECTURE 5-Flow Control-Looping
Department of ICT
Online Material
Lecture 5: Flow Control - Looping
Facilitator
B.Hemalatha
Assistant Professor
123456789
123456789
123456789
123456789
Nested For Loop
for loop inside for loop in PHP, it is known as nested for loop.
The inner for loop executes only when the outer for loop
condition is found true.
In case of inner or nested for loop, nested for loop is executed
fully for one outer for loop.
If outer for loop is to be executed for 3 times and inner for loop
for 3 times, inner for loop will be executed 9 times (3 times for
1st outer loop, 3 times for 2nd outer loop and 3 times for 3rd
outer loop).
Nested For Loop
<?php
for($i=1;$i<=3;$i++){
for($j=1;$j<=3;$j++){
11
echo "$i $j<br/>"; 12
13
21
} 22
23
} 31
32
?> 33
For Each Loop
for each loop is used to Example
traverse array elements. <?php
Syntax $season=array("summer","winte
foreach( $array as $var ) r","spring","autumn");
{ foreach( $season as $arr ){
} }
//code to be executed
Syntax
//code to be executed
}
Flowchart
Example 1
PHP program to print array elements using foreach loop.
<?php
//declare array
echo "$element";
?>
Example 2
PHP program to print associative array elements using
foreach loop.
<?php
//declare array
$employee = array (
echo "</br>";
?>
Name : Alex
Email : [email protected]
Age : 21
Gender : Male
Example 3
Multi-dimensional array
<?php
//declare multi-dimensional array
$a = array();
$a[0][0] = "Alex";
$a[0][1] = "Bob";
$a[1][0] = "Camila";
$a[1][1] = "Denial";
Example 3
//display multi-dimensional array elements through foreach loop
foreach ($a as $e1) {
foreach ($e1 as $e2) {
echo "$e2\n";
}
}
?>
?>
Alternative Example
<?php
1
$n=1; 2
3
while($n<=10): 4
5
echo "$n<br/>"; 6
7
8
$n++; 9
10
endwhile;
?>
Example
Example of printing alphabets using while loop.
<?php
$i = 'A';
while ($i < 'H') A
{ B
C
echo $i; D
$i++; E
echo "</br>"; F
G
}
?>
Nested While Loop
While loop inside another while loop in PHP, it is known as
nested while loop.
In case of inner or nested while loop, nested while loop is
executed fully for one outer while loop.
If outer while loop is to be executed for 3 times and nested
while loop for 3 times, nested while loop will be executed 9
times (3 times for 1st outer loop, 3 times for 2nd outer loop
and 3 times for 3rd outer loop).
Example
<?php
$i=1;
11
12
while($i<=3){ 13
21
$j=1; 22
23
while($j<=3){ 31
32
echo "$i $j<br/>"; 33
$j++; }
$i++;
?>
do-while loop
Is used to execute a set of code of the program several
times.
If you have to execute the loop at least once and the
number of iterations is not even fixed, it is recommended
to use the do-while loop.
It executes the code at least one time always because the
condition is checked after executing the code.
The do-while loop is very much similar to the while loop
except the condition check.
do-while loop
The main difference between both loops is that while loop
checks the condition at the beginning, whereas do-while
loop checks the condition at the end of the loop.
Syntax
do{
//code to be executed
}while(condition);
Example
<?php
$n=1;
1
2
do{ 3
4
echo "$n<br/>"; 5
6
$n++; 7
8
}while($n<=10); 9
10
?>
Difference between while and do-while loop
The while loop is also named as The do-while loop is also named
entry control loop. as exit control loop.
The body of the loop does not The body of the loop executes at
execute if the condition is false. least once, even if the condition is
false.
This loop does not use a semicolon Do-while loop use semicolon to
to terminate the loop. terminate the loop.
Declare
Declare statement allows you to specify execution directives
for a block of code.
The structure of a declare statement is:
declare (directive)
statement
Currently, there is only one declare form, the ticks directive.
register_tick_function("some_function");
declare(ticks = 3) {
{ // do something
}
In this code, some_function( ) is called after every third
statement is executed
Exit and return
▪ The exit statement ends execution of the script as soon as it is
reached.
▪ The return statement returns from a function or (at the top level
of the program) from the script.
▪ The exit statement takes an optional value.
if (!$handle)
$i = 1;
______($i < 6)
echo $i;
$i++;
Assignment
Create a loop that runs from 0 to 9.
echo $i;
}
Assignment
Create a loop that runs from 0 to 9.
echo $x;
}
Do It Yourself
Click
THANK YOU