Flow Control in PHP
Session 9
Review
Conditional statements execute a set of
statements only when a specified condition
is satisfied
if statement executes a block of code only
when the specified condition is true
switch statement executes a block of code
based on the value it matches
switch statement checks single variable
against multiple values
Ternary operator simplifies complex
conditions into one line statements
PHP / Session 9 / Slide 2 of 29
Objectives
Use while loop
Use do-while loop
Use for loop
Use jump statements
PHP / Session 9 / Slide 3 of 29
Loops
Executes a block of statements repetitively
Tests and executes loop statements repetitively
if the condition returns true value
Stops executing loop statements if the
condition returns false value
Types of loop statements are:
while loop
do-while loop
for loop
PHP / Session 9 / Slide 4 of 29
While Loop - I
Executes
the
loop
executing
statements
depending on the return result of the testing
condition
Syntax for while loop is:
while(condition)
{
Code executes if the condition is true;
}
Code executes if the condition is false;
PHP / Session 9 / Slide 5 of 29
While Loop - II
The while keyword is followed by the
condition in parentheses
The
condition
is
the
test
expression
consisting of variables and operators. If
the condition is satisfied, the code in the
loop body is executed. If the condition is
false, the loop stops and the control move
to the statement following the loop
The value of the executing statements
depends on the condition, therefore it
checks the condition after each iteration of
the loop
PHP / Session 9 / Slide 6 of 29
Example for While Loop - I
For example, to display first 10 odd numbers
$number=1;
echo “The odd numbers are:<BR>”;
while($number <= 10)
{
echo “<BR>$number<BR>”;
$number=$number+2;
}
?>
PHP / Session 9 / Slide 7 of 29
Example for While Loop - II
Here, the first 10 odd numbers are
displayed using the while loop
The value keeps displaying until the
counter reaches to the value 10
The loop stops once the condition gets
satisfied
PHP / Session 9 / Slide 8 of 29
Do-While Loop - I
Works similarly like the while loop
Difference between the while and do-while
loop is that in the do-while loop, the
condition is placed at the end of the loop
Syntax for a do-while loop is:
do
{
Code executes if the condition is true;
}
while(condition);
Code executes if the condition is false;
PHP / Session 9 / Slide 9 of 29
Do-While Loop - II
In do-while loop, the loop body follows the
do keyword
The loop body is executed at least once. The
loop body is followed by the while keyword
and the condition is in the parenthesis
If the condition returns true value, the
loop keeps executing. If the condition
returns false value, the loop ends and the
statements following the while keyword are
executed
PHP / Session 9 / Slide 10 of 29
Example for Do-While Loop - I
For example, to print the first 10 even
numbers
$counter=0;
echo “The even numbers are:<BR><BR>”;
echo “$counter<BR>”;
do
{
echo “$counter<BR>”;
$counter=$counter+2;
}while($counter <= 10);
echo “<BR>The value could not be displayed”;
?>
PHP / Session 9 / Slide 11 of 29
Example for Do-While Loop - II
Here, the even numbers are displayed
using the do-while loop
Before the iteration of the loop starts,
the value is displayed once. Then the
execution starts
The value keeps displaying until the
counter reaches to the value 10. The
loop stops once the condition gets
satisfied
PHP / Session 9 / Slide 12 of 29
For Loop
Enables us to execute a block of
code repetitively for a fixed
number of times
Executes the loop executing
statements till the testing
condition gets satisfied
Stops executing once the
condition gets satisfied
PHP / Session 9 / Slide 13 of 29
Using For Loop - I
Syntax for the for loop is:
for(initialization;condition;reinitialization)
{
Code executes if the condition is true;
}
Code executes if the condition is false;
PHP / Session 9 / Slide 14 of 29
Using For Loop - II
The condition specifies the testing
condition
The initialization initializes the value
for the counter
The re-initialization specifies the
increment and decrement of the counter
The codes inside the loop gets executed
only when the condition returns true value
The re-initialization statement helps in
the iteration of the loop
PHP / Session 9 / Slide 15 of 29
Example for the for loop - I
For example, to display the output
multiplication of a number by 2
of
the
$number=6;
for($counter=1;$counter <= 6;$counter++)
{
echo “<BR>$number<BR>”;
$number=$number*2;
}
echo “Values could not be calculated.”;
?>
PHP / Session 9 / Slide 16 of 29
Example for the for loop - I
Here, the multiplication value of 6
with 2 is displayed
The loop keeps executing until the
counter value reaches to 6
Once the counter value reaches to 6,
the loop stops executing. The counter
keeps incrementing from 0 to 6
PHP / Session 9 / Slide 17 of 29
Jump Statements
Controls the execution of the loop
statements
Different jump statements are:
break statement
continue statement
exit statement
PHP / Session 9 / Slide 18 of 29
Break Statement
Stops the execution of the loop
Passes the control either to the
beginning of the next loop or to
the statement following the loop
Uses with the if statement,
switch statement, for loop, while
loop, and do-while loop
PHP / Session 9 / Slide 19 of 29
Example for break statement - I
For example, to display the consecutive numbers
from 1 to 10 by using for loop
for($i=1;;$i++)
{
if($i>10)
{
break;
}
echo $i;
}
?>
PHP / Session 9 / Slide 20 of 29
Example for break statement - II
Here, the break statement is used
with the for loop
The condition is specified in the
if statement
The break statement controls the
loop. If the break statement is not
used, then the loop will go in
continuous state
PHP / Session 9 / Slide 21 of 29
Continue Statement
Uses with the loop statements
Stops executing the current loop
and continues with the next
execution of the program
Uses with the if statement, for
loop, while loop, and do-while loop
PHP / Session 9 / Slide 22 of 29
Example for continue statement I
For example, to display numbers from 1 to 10
with while loop
$counter = 0;
while($counter<10)
{
$counter++;
if($counter==5)
{
echo “Continues the loop”;
continue;
}
echo “$counter<BR><BR>”;
}
echo “The loop ends here”;
?>
PHP / Session 9 / Slide 23 of 29
Example for continue statement II
Here, the continue statement is used for
checking the condition in the if statement
The counter initializes to 0. The loop
continues till the counter value becomes
5. As the counter value becomes 5, the
loop skips executing the value 5 and
starts executing from the next counter
value
The loop continues till the condition
becomes false
PHP / Session 9 / Slide 24 of 29
Exit Statement
Ends the loop and passes the
control to the statement
following the body of the loop
For example, to display the HRA
value using if statement with
the exit statement
PHP / Session 9 / Slide 25 of 29