Chapter 3 Control Structures in php.pptx

MahadHonest1 8 views 26 slides Oct 19, 2025
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

Php


Slide Content

Chapter 3 Control Structures

Chapter Outlines Types of Control Structures Conditional Control Structures if Statement if else statement if elseif statement switch Statement 2 Loop Control Structures While Loop do...while Loop for Loop break and continue Statements Nested Loops

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

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

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

if Statements Example $month = "March"; if ($month == "March") echo "It's springtime"; $a = 2; $b = 3; if ($a > $b) echo "$a is greater than $b"; 6 if ($a < $b) echo "$a is less than $b"; if ($a >= $b) echo "$a is greater than or equal to $b"; if ($a <= $b) echo "$a is less than or equal to $b";

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

if else statement Examples $marks = 48; if ($marks >= 50) echo "Pass"; else echo "Not pass"; 8

if elseif statement (1 of 2) if (expression) statement(s) elseif (expression) statement(s) else if (expression) statement(s) else statement(s) 9

if elseif statement (2 of 2) Note that braces are required when if block contains more than one statement . 10

if elseif statement Example $marks = 87; if ($marks >= 90) echo "Excellent"; elseif ($marks >= 80 echo "Very good"; elseif ($marks >= 50) echo " minimal pass " ; else echo " not pass " ; 11

switch Statements (1 of 2) The switch statement is useful where one variable, or the result of an expression, can have multiple values, each of which should trigger a different activity. 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 because a condition has been fulfilled, exit the switch statement and jump to the following statement. 12

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 . If you prefer, you may replace the first curly brace in a switch statement with a single colon and the final curly brace with an endswitch command. However, this approach is not commonly used and is mentioned here only in case you encounter it in third-party code. 13

Differentiate b/w if/ elseif & switch What is the main difference between if/ elseif & switch statements? 14

Example $answer = 'N'; switch ( $answer ) { case ( $answer == ‘y’ || $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"); } 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. 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. 16

Types of Loop While Do While For Differentiate between them Which one you most like to use in your program? 17

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

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

do...while Loop 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 . 20

Example $count = 1; do { echo $count . "< br >"; $count++; } while ($count <= 5); 21

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

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

for Loops Example for ($i = 1; $i <= 5; $i++) { echo $i . "<br>"; } 24

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

End of Chapter 3 26
Tags