You are on page 1of 31

Chapter 2

Control Structures
Two types
• Conditional control structures
• Loop control structures

2
• The conditional control structures affect the flow
of the program and execute or skip certain code
according to certain criteria.

3
• Loop control structures execute certain code a
number of times according to specified criteria.

4
Conditional Control Structures
• Conditional control structures are important in
allowing your program to take different execution
paths based on decisions it makes at runtime. PHP
supports both the if and switch conditional control
structures.

5
if Statements
The expression in the if statement is referred to as the
truth expression. If the truth expression evaluates to
true, the statement or statement list following it are
executed; otherwise, they are not.
if (expression)
statement(s)

6
• You can add an else branch to an if statement to
execute code only if all the truth expressions in
the if statement evaluated to false.

7
if else statement

if (expression)
statement(s)
else
statement(s)

8
if elseif statement
if (expression)
statement(s)
elseif (expression)
statement(s)
else
statement(s)

9
• Note that braces are required when if block
contains more than one statement.
• Also it is a common practice by PHP developers
to use else if notation instead of elseif.

10
switch Statements
• You can use the switch construct to replace
certain lengthy if/elseif constructs. It is given an
expression and compares it to all possible case
expressions listed in its body.

11
• Use the break statement to end execution and
skip to the code following the switch construct.

12
• If no case expression is met and the switch
construct contains default, the default
statement list is executed. Note that the default
case must appear last in the list of cases or not
appear at all.

13
Example

$answer = 'N';
switch ( $answer ) {
case ( $answer == 'y' or $answer == 'Y' ):
echo ("The answer was yes");
break;

14
case ( $answer == 'n' or $answer == 'N' ):
echo ("The answer was no");
break;
default:
echo ("Error: $answer is not a valid answer");
}

15
Loop Control Structures
• Loop control structures are used for repeating
certain tasks in your program, such as iterating
over a database query result set.

16
Essentials of Loops
• The name of a loop variable (or loop counter)
• The initial value of the loop variable
• The loop-continuation condition that tests for
the final value of the loop variable

17
Essentials of Loops (cont…)

• The increment (or decrement) by which the


loop variable is modified each time through
the loop.

18
Types of Loop
• While
• Do While
• For

Differentiate between them

19
Example
$i = 1;
echo ("Natural numbers from 1to15 are: ");
while ( $i <= 15 )
{
echo "$i, ";
$i++;
}

20
break and continue Statements

• break statement is used to terminate the


execution of a loop in the middle of iteration.
• continue statement is used to stop the execution
of specific loop iteration and begin executing the
next one.

21
Example of break statement
$i = 1;
while ($i <= 15)
{
echo "$i, ";
$i++;
if ( $i == 12 )
break;
}

22
Example of continue

$i = 0;
while ($i <= 15)
{
$i++;
if ( $i % 5 == 0 )
continue;
else
echo "$i, ";
}

23
do...while Loops
• The do...while loop is similar to the previous while
loop, except that the truth expression is checked at
the end of each iteration instead of at the beginning.
This means that the loop always runs at least once.

24
Example
$result = 1;
$n = 5;
echo ("Factorial of $n = ");
do{
$result *= $n; //$result *= $n--;
$n--;
}while ($n > 0);
echo "$result"; //you can use without quots

25
for Loops

• The for loop accepts three arguments:


for (start_expressions; truth_expressions;
increment_expressions)
The start expression is evaluated only once when the loop is
reached. Usually it is used to initialize the loop control variable.

26
The truth expression
• The truth expression is evaluated in the
beginning of every loop iteration. If true, the
statements inside the loop will be executed; if
false, the loop ends.

27
The increment/decrement expression

• The increment expression is evaluated at the end


of every iteration before the truth expression is
evaluated. Usually, it is used to increment the loop
control variable, but it can be used for any other
purpose as well.

28
• Both break and continue behave the same way
as they do with while loops.

29
Example

for ($i = 1; $i <= 10; $i++) {


echo "The square of $i is ". $i*$i ."<br>";
}
Notice the use of concatenation operator ( . ).

30
End of Chapter 2

31

You might also like