Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITM 352 Flow-Control: if and switch

Similar presentations


Presentation on theme: "ITM 352 Flow-Control: if and switch"— Presentation transcript:

1 ITM 352 Flow-Control: if and switch

2 What is "Flow of Control"? Flow of Control is the execution order of instructions in a program All programs can be written with three control flow elements: 1. Sequence - just go to the next instruction 2. Selection - a choice of at least two either go to the next instruction or jump to some other instruction 3. Repetition - a loop (repeat a block of code) at the end of the loop either go back and repeat the block of code or continue with the next instruction after the block Selection and Repetition are called Branching since these are branch points in the flow of control

3 The Type boolean A primitive type
Can have expressions, values, constants, and variables just as with any other primitive type Only two values: true and false Every expression is boolean in some way 0, 0.0, '0', '' are all false Anything other than the above is true Comparison operators always return boolean $is_desired_grade = ($grade == 'A'); $is_drinking_age = ($age >= 21); $not_graduating = ($year != 'senior');

4 Boolean Expressions Boolean expressions can be thought of as test conditions (questions) that are either true or false Often two values (numbers, strings) are compared, return value is a boolean (i.e. true or false) For example: Is A greater than B?, Is A equal to B?, Is A less than or equal to B? Comparison operators are used for boolean expressions (<, >, <=, >=, ==, ===, !=. !==, …) A and B can be any data type (or class), but they generally are a "compatible" data type (or class) Comparisons are either numeric or lexicographic but can be user-defined via objects and functions. Comparing non-compatible types is legal but may have unexpected results.

5 Basic PHP Comparison Operators

6 A Note on Printing Boolean Values
The echo (and print) command will convert values to strings for printing true is converted to '1' false is converted to “” (the empty string) echo true 1 echo false <no output>

7 "Identical" Comparison Operators
Sometimes you really want to be sure two values are exactly the same value and type Is 0.0 equal to 0? Use the '===' and '!==' to test equality and non-equality for both value and type 0.0 == 0 returns true 0.0 === 0 returns false Do lab exercise #1

8 Compound Boolean Expressions
Use && to AND (intersect) two or more conditions Use || to OR (union) two or more conditions For example, write a test to see if B is either 0 or between the values of A and C : (B == 0) || (A <= B && B < C) In this example the parentheses are not required but are added for clarity Subject to Precedence rules

9 Truth Tables for boolean Operators
&& (and) || (or) Value of A Value of B A && B true true true true false false false true false false false false Value of A Value of B A || B true true true true false true false true true false false false Value of A !A true false false true ! (not)

10 Precedence Rules for Common Operators
Highest Precedence the unary operators: ++, --, and ! the binary arithmetic operators: *, /, % the binary arithmetic operators: +, - the boolean operators: <, >, =<, >= the boolean operators: ==, != the boolean operator & the boolean operator | the boolean operator && the boolean operator || Lowest Precedence Do lab exercise #2

11 PHP Flow Control Statements
Branching: Selection if if-else if-else if-else if- … - else switch Branching: Repetition while do-while for foreach Sequential the default PHP automatically executes the next instruction unless you use a branching statement

12 PHP if statement Simple decisions
Do the next statement if test is true or skip it if false Syntax: if (Boolean_Expression) Action if true; //execute if true next action; //always executed Note the indentation for readability (not compiler or execution correctness)

13 if Example The body of the if statement is conditionally executed
if ($eggsPerBasket < 12) { //begin body of the if statement echo "Less than a dozen eggs per basket"; //end body of the if statement } $totalEggs = $numberOfEggs * $eggsPerBasket; echo "You have a total of $totalEggs eggs."; The body of the if statement is conditionally executed

14 PHP Statement Blocks: Compound Statements
Action if(true) can be either a single statement or a set of statements enclosed in curly brackets (a compound statement, or block). For example: All statements between braces are controlled by if if ($eggsPerBasket < 12) { //begin body of the if statement echo "Less than a dozen ..."; $costPerBasket = 1.1 * $costPerBasket; } //end body of the if statement $totalEggs = $numberOfEggs * $eggsPerBasket; echo "You have a total of $totalEggs eggs.");

15 PHP Statement Blocks: Compound Statements
Alternatively, "if(<exp>): … endif; " also works: All statements between : and endif; are controlled by if if ($eggsPerBasket < 12) : //begin body of the if statement echo "Less than a dozen ..."; $costPerBasket = 1.1 * $costPerBasket; //end body of the if statement endif; $totalEggs = $numberOfEggs * $eggsPerBasket; echo "You have a total of $totalEggs eggs."); This style is useful when using PHP in large blocks of HTML (see example in textbook) Do lab exercise #3

16 Gotchas - 1 Statements after the body of the if statement always execute (not conditional unless grouped inside {}'s) Let's say you want to give free shipping and a 10% discount if the order is $50: Wrong: if ($total > 50) $shipping = 0; $total = $total *0.9; Right: if ($total > 50) { $shipping = 0; $total = $total *0.9; }

17 Gotchas - 2 Never put a ; after the if statement!
What will be the values of $total and $shipping after the if test below? Why? $total = 40; if ($total > 50); { $shipping = 0; $total = $total *0.9; }

18 Two-way Selection: if-else
Select either one of two options Either do Action1 or Action2, depending on test value Syntax: if (Boolean_Expression) { Action1 //execute only if Boolean_Expression true } else Action2 //execute only if Boolean_Expression false Action3 //anything here always executed

19 if-else Examples Example with single-statement blocks:
if ($time < $limit) echo "You made it."; else echo "You missed the deadline."; Example with compound statements: { $bonus = 100; } $bonus = 0; Do lab exercise #4

20 Multibranch selection: if-else if-elseif-…-else
One way to handle situations with more than two possibilities Syntax: if(Boolean_Expression_1) { Action_1 } elseif(Boolean_Expression_2) { Action_2 } . elseif(Boolean_Expression_n) { Action_n } else { Default_Action }

21 if-elseif-elseif-…-else Example
if($score >= 90 && $score <= 100) $grade= 'A'; elseif ($score >= 80) $grade= 'B'; elseif ($score >= 70) $grade= 'C'; elseif ($score >= 60) $grade= 'D'; else $grade= 'E'; Note how the sequence is important here and must use elseif rather than just if (why?)

22 if-elseif-elseif-…-else Non-Example??
if ($profRel == "colleague" ) $greeting = "Thomas"; elseif ($profRel == "friend" ) $greeting = "Tom"; elseif ($profRel == "grad student" ) $greeting = "TC"; elseif ($profRel == "undergrad student" ) $greeting = "professor"; else $greeting = "Dr. Collins";

23 Use switch for single-variable if
switch($profRel) { case "colleague" : $greeting = "Thomas"; break; case "friend" : $greeting = "Tom"; case "grad student" : $greeting = "TC"; case "undergrad student" : $greeting = "professor"; default : $greeting = "Dr. Collins"; } Do lab exercise #5


Download ppt "ITM 352 Flow-Control: if and switch"

Similar presentations


Ads by Google