Chapter 3 Control Structures

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

Chapter 3

Control Structures
Types of Control Structures
• Sequential Control structure (default structure)
Execution by which statements are executed in the same
order in which they appear in the program.
• Conditional control structure
Affect the flow of the program and execute or skip certain
code according to certain criteria.
• Loop control structure
Execute certain code a number of times according to specified
criteria.
2
Conditional Control Structures
• Conditional control structures allows your program to
take different execution paths based on decisions it
makes at runtime.
• PHP supports both the if and switch conditional
control structures.

3
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)

4
if else statement
You can add an else branch to an if statement to
execute code only if the truth expressions in the if
statement evaluated to false.
if (expression)
statement(s)
else
statement(s)

5
if elseif statement (1 of 2)
if (expression)
statement(s)
elseif (expression)
statement(s)
else if (expression)
statement(s)
else
statement(s)
6
if elseif statement (2 of 2)
• 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.

7
switch Statements (1 of 2)
• 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.
• Use the break statement to end execution and skip to
the code following the switch construct.

8
switch Statements (2 of 2)
• 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.

9
Example
$answer = 'N';
switch ( $answer ) {
case ( $answer == 'y' or $answer == 'Y' ):
echo ("The answer was yes");
break;
case ( $answer == 'n' or $answer == 'N' ):
echo ("The answer was no");
break;
default:
echo ("Error: $answer is not valid");
}
10
Differentiate b/w if/elseif & switch
• What is the main difference between if/elseif & switch
statements?

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

12
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
• The progress (increment /decrement) by
which the loop variable is modified each time
through the loop.
13
Types of Loop
• While
• Do While
• For

Differentiate between them


Which one you most like to use in your program?

14
While Loop
• The truth expression of while loop is checked at the
beginning of each iteration.
• As with if statements, you will notice that curly braces
({ }) are required to hold the statements inside the
while statements, unless there’s only one.

15
Example
$i = 1;
while ( $i <= 15 )
{
echo "$i, ";
$i++;
}

16
do...while Loops
• The do...while loop is similar to 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.

17
Example
$result = 1;
$n = 5;
do {
$result *= $n;
$n--;
} while ($n > 0);
echo $result;

18
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.

19
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.

20
The increment/decrement expression
• The increment expression is evaluated at the end of
every iteration before starting the next iteration.
Usually, it is used to increment the loop variable, but
it can be used for any other purpose as well.

21
Example
for ($i = 1; $i <= 10; $i++) {
echo "The square of $i is ". $i*$i ."<br>";
}

• Notice the use of concatenation operator ( . ).

22
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.

23
Example of break statement
$i = 1;
while ($i <= 15)
{
echo "$i, ";
$i++;
if ( $i == 10 )
break;
}
24
Example of continue statement
$i = 0;
do {
$i++;
if ( $i % 2 == 0 )
continue;
else
echo "$i, ";
} while ($i <= 15);

25
Nested Loops
• You can nest loops inside one another (loop within
another loop). The next slide example prints
multiplication table using nested two loops (you can
format the output in a table).

26
Example
for ($i = 1; $i <= 12; $i++) //row loop
for ($j = 1; $j <= 12; $j++) //column loop
echo ($i * $j);

27
End of Chapter 3

28

You might also like