MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic...

21
MS3304: Week 6 Conditional statements

Transcript of MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic...

MS3304: Week 6

Conditional statements

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

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

Conditional statements

• Conditional statements allow the script to decide which statement will be executed next

• Conditional statements in PHP are:– if– else– elseif

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

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

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?

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

Comparative and logical operators example 1if ($CW1=="NS"){

echo "CW1: 0 - <b>Resit</b>(not submitted). <br>\n";}if($CW1<30){

echo "CW1: $CW1 - <b>Resit</b> (below 30) .<br>\n";}if(($CW1>29) && ($CW1<40)){

echo "CW1: $CW1 - not passed, but threshold met.<br>\n";

}if ($CW1<40){

echo "CW1 passed.<br>\n";}

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.";}

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.";

}

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

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.";

}

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.

if/elseif statementsif ($CW1=="NS"){echo "CW1: 0 - <b>Resit</b> (not submitted).<br>\n";

}elseif($CW1<30){echo "CW1: $CW1 - <b>Resit</b> (below 30) .<br>\n";

}elseif(($CW1>29) && ($CW1<40)){echo "CW1: $CW1 - not passed, but threshold met.<br>\n";

}else{echo "CW1: $CW1 - passed.<br>\n";

}

Nested if statements

if (condition){ if(condition){

statements} else{

statements}

} else{statements

}

Nested if statements example

The Marks Calculator

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

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

Advanced topic:The switch statementswitch ($catGender){case "Male" :

$catPronoun = "He";break;

case "Female" : $catPronoun = "She"; break;

default : $catPronoun = "The cat";

}

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