Download presentation
Presentation is loading. Please wait.
Published byLeon Curtis Modified over 9 years ago
1
MS3304: Week 6 Conditional statements
2
Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making statements in PHP Complex decision making statements in PHP Other uses of if statements Advanced topic introduction: the switch statement
3
Program flow of control Order of statement execution is called the flow of control Default order of execution of programming statements is linear Some statements (conditional) can modify the order of execution Conditional statements are based on Boolean logic
4
Conditional statements Conditional statements allow the script to decide which statement will be executed next Conditional statements in PHP are: –if –else –elseif
5
Overview of an the if statement The basic if statement has the following syntax if (condition){ statement; } if is a PHP reserved word The condition is a Boolean expression that can be evaluated as true or false If the condition is true, this statement is executed
6
Boolean logic Developed by George Boole in mid 1800s All Boolean logic is based on the evaluation of statements These statement can either be evaluated as true or false Conditional and logical operators are used to create these statements
7
Boolean logic example Assuming we have the following variables: $CW1 $CW2 $CWAverage = ($CW1+$CW2)/2 What logical statements, in plain English, can we make to tell students at UEL if they have passed or need to do a resit?
8
Comparative and logical operators Comparative ==equal to !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to Logical !NOT &&AND ||OR
9
Comparative and logical operators example 1 if ($CW1=="NS"){ echo "CW1: 0 - Resit (not submitted). \n"; } if($CW1<30){ echo "CW1: $CW1 - Resit (below 30). \n"; } if(($CW1>29) && ($CW1<40)){ echo "CW1: $CW1 - not passed, but threshold met. \n"; } if ($CW1<40){ echo "CW1 passed. \n"; }
10
Comparative and logical operators example 2 if ( ($CW1<30 || $CW1 == "NS") || ($CW2<30 || $CW2 == "NS") || ($CWAverage <40) ){ echo "You have not passed the module and must resit at least one of the of the pieces of CW."; }
11
Comparative and logical operators example 3 if ( ($CW1>=30 && $CW1!="NS") && ($CW2>=30 && $CW2!="NS") && ($CWAverage>=40) ){ echo "You have passed the module with a mark of $CWAverage."; }
12
Overview of an the if/else statement The basic if/else statement has the following syntax if (condition){ statements; } else{ statements; } If the condition is true these statements are executed If the initial condition is false then the second block of statements is executed
13
if/else statements if ( ($CW1>=30 && $CW1!="NS") && ($CW2>=30 && $CW2!="NS") && ($CWAverage>=40) ){ echo "You have passed the module!" }else{ echo "You have not passed the module and must resit at least one of the of the pieces of CW."; }
14
Overview of an the if/elseif statement The basic if/else statement has the following syntax if (condition){ statements; } elseif (condition){ statements; } else{ statements; } If the condition is true these statements are executed If neither of the conditions are true, then these statements are executed If the first condition is false it tests the second condition, its true these statements are executed.
15
if/elseif statements if ($CW1=="NS"){ echo "CW1: 0 - Resit (not submitted). \n"; }elseif($CW1<30){ echo "CW1: $CW1 - Resit (below 30). \n"; }elseif(($CW1>29) && ($CW1<40)){ echo "CW1: $CW1 - not passed, but threshold met. \n"; }else{ echo "CW1: $CW1 - passed. \n"; }
16
Nested if statements if (condition){ statements } else{ statements } } else{ statements }
17
Nested if statements example The Marks Calculator
18
Other uses of if statements if ($catGender=="Male" ){ $catPronoun = "He"; }elseif ($catGender=="Female" ) { $catPronoun = "She"; } else { $catPronoun = "The cat"; } echo "I have a $catGender $catColour cat named $myCat. $catPronoun is $catAge years old."
19
If statements your turn Write the PHP statements to –Take both a cat’s age in years and a humans as input passed from a form –Calculates the age of the cat in human years and displays it –Displays a statement saying if the cat is older or younger than the person in cat equivalent years Cat age calculation formula details Year 1 = 15 human years Year 2 = 9 human years Years 3+ = 4 human years per year
20
Advanced topic: The switch statement switch ($catGender){ case "Male" : $catPronoun = "He"; break; case "Female" : $catPronoun = "She"; break; default : $catPronoun = "The cat"; }
21
PHP conditional statements summary Used to control the flow of execution of statements in a script Based on the evaluation of a Boolean statement through the use of comparative and logical operators Can use the if, elseif and else statements to compose more logically complex statements Can be nested within each other
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.