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
Trang 1Flow Control in PHP
Session 9
Trang 2 Conditional statements execute a set of
statements only when a specified condition
switch statement checks single variable
against multiple values
Ternary operator simplifies complex
Trang 3 Use do-while loop
Use for loop
Trang 4 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:
Trang 5While Loop - I
Executes the loop executing statements
depending on the return result of the testing
Trang 6While Loop - II
condition in parentheses
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
depends on the condition, therefore it checks the condition after each iteration of the loop
Trang 7Example for While Loop - I
Trang 8Example 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
Trang 9Do-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
Trang 10Do-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
Trang 11Example for Do-While Loop - I
For example, to print the first 10 even
Trang 12Example for Do-While Loop - II
using the do-while loop
the value is displayed once Then the
execution starts
counter reaches to the value 10 The
loop stops once the condition gets
satisfied
Trang 13For Loop
code repetitively for a fixed
number of times
statements till the testing
condition gets satisfied
Trang 14Using For Loop - I
Trang 15Using 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
Trang 16Example for the for loop - I
For example, to display the output of the multiplication of a number by 2
Trang 17Example for the for loop - I
with 2 is displayed
counter value reaches to 6
the loop stops executing The counter keeps incrementing from 0 to 6
Trang 19Break Statement
beginning of the next loop or to
the statement following the loop
switch statement, for loop, while
loop, and do-while loop
Trang 20Example for break statement - I
For example, to display the consecutive numbers
from 1 to 10 by using for loop
Trang 21Example 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
Trang 22Continue 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
Trang 23Example for continue statement - I
For example, to display numbers from 1 to 10
with while loop
Trang 24Example 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
Trang 25Exit 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
Trang 26Example for exit statement - I
Trang 27Example for exit statement - II
on the basis of the basic salary
6000, then the if statement exits
than or equal to 6000, then the
HRA value is calculated
Trang 28Summary - I
false values returned by testing the testing condition
depending on the return result of the
testing condition after testing it
placed after the loop ends In the first
iteration of the loop, the statements placed inside the loop get executed without any
consideration of the testing result
Trang 29Summary - II
The break statement stops the iteration of
the loop from the current loop execution
This statement is used with for loop, while
loop, and do…while loop statements
The continue statement is used for breaking
the iteration of the loop from the current
loop execution
The exit statement is used to break the loop
while the loop is in the execution process
After using the exit statement, no further
execution of the loop is possible