Presentation is loading. Please wait.

Presentation is loading. Please wait.

CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.

Similar presentations


Presentation on theme: "CONTROL STRUCTURE The if, elseif, and else & switch Statements 1."— Presentation transcript:

1 CONTROL STRUCTURE The if, elseif, and else & switch Statements 1

2 The if, elseif, and else Statements  The fundamentals of Boolean comparison  Conditional expressions and operators  How to combine expressions with logical operators  How to nest conditionals 2

3 if Conditionals  In the simplest sense, all decisions are made based on a condition: something that may or may not be true  The if statement follows this syntax: if (condition) { statements } 3

4 example if Conditionals <?php $count=10; // Condition 1 if ($count==10) { echo “Condition 1 outputs this.”; } ?> 4

5 If-else Conditionals  The if statement follows this syntax: if (condition) { Statements is true } else { Statements is false } 5

6 example if-else Conditionals <?php $count=5; // Condition is true if ($count==10) { echo “Condition 1 outputs this TRUE.”; } // Condition is false { echo “Condition 2 outputs this FALSE.”; } ?> 6

7 If-elseif-else Conditionals  The if statement follows this syntax: if (condition1) { Statements is true condition1 } elseif (condition2) { Statements is true condition2 } else { Statements is false } 7

8 example if-elseif-else Conditionals <?php $count=5; if ($count<=0)// Condition1 { echo “Condition 1 outputs this TRUE.”; } if ($count>5)// Condition2 { echo “Condition 1 outputs this TRUE.”; } else// Condition is false { echo “Other Condition outputs this FALSE.”; } ?> 8

9 Exercise – ifelse.php  For example, let’s assume you want to place someone into a four-category age-grouping system. The age groups are divided as follows:  Group A—Ages 20 and younger  Group B—Ages 21 to 40  Group C—Ages 41 to 60  Group D—Ages 61 and older  Give input value age from keyboard and display on monitor.  POST value on safe from 9

10 The Comparison Operators  The syntax for conditional operators is the same as other binary operators; that is, one value should be placed on each side of the operator, as shown here: Operator Name Example == Equal $var1 == $var2 === Identical $var1 === $var2 != Not equal $var1 != $var2 !== Not identical $var1 !== $var2 > Greater than $var1 > $var2 >= Greater than or equal to $var1 >= $var2 < Less than $var1 < $var2 <= Less than or equal to $var1 <= $var2 10

11 The == and === operators  The == and === operators (and their != and !== relatives) aren’t the same. The first, == which checks two values for equality, compares two values of the same data type. If they aren’t the same type, == typecasts them before they are compared. Therefore, the string “1” and the integer 1 are the same.  On the other hand, === checks each value’s type before comparing the actual value. If the types don’t match, the variables aren’t considered the same, and false is returned.  Only if two variables are identical (that is, their type and value both match) will this operator return true. 11

12 the Logical Operators  The && operator returns true if and only if both operands are true. Operator Name Example AND And $var1 and $var2 OR Or $var1 or $var2 XOR Exclusive or $var1 xor $var2 && And $var1 && $var2 || Or $var1 || !Not!$var1 12

13 AND operator in detail.  The AND (or &&) Operator Requires That Both of the Values Be True Left Value Right Value Return Value True True True True False False False True False False False False 13

14 OR operator in detail.  The OR (or ||) Operator Requires That at Least One of the Values Be True Left Value Right Value Return Value True True True True False True False True True False False False 14

15 XOR operator in detail.  The XOR Operator Requires That Only One of the Values Be True. Left Value Right Value Return Value True True False True False True False True True False False False 15

16 Not operator in detail.  The NOT (or !) Operator Reverses the Value of a Boolean Value Right Value Return Value True False False True 16

17 The switch Statement  The differences between switch and if-elseif-else  The syntax for the switch statement  How to break switch statement execution  How to specify a default case  How to create multifunction pages 17

18 switch Statement  A switch statement is used to compare a single variable with multiple possible values.  Here’s the syntax for a switch statement: switch (variable) { case value:code; break; case value:code; break; default:code; break; } 18

19 If-elseif-else program <?php /* ex01.php – demonstrates age group output using an if-elseif-else clause */ $chrAgeGroup = ‘B’; // Specify age group if ($chrAgeGroup == ‘A’) // Group A: 20 and younger { echo “Ages 20 and younger”; } elseif ($chrAgeGroup == ‘B’) // Group B: 21 – 40 { echo “Ages 21 to 40”; } elseif ($chrAgeGroup == ‘C’) // Group C: 41 – 60 { echo “Ages 41 to 60”; } else // Group D: 61 and older { echo “Ages 61 and older”; } ?> 19

20 <?php $chrAgeGroup = ‘B’; switch ($chrAgeGroup) { case ‘A’: // Group A: 20 and younger echo “Ages 20 and younger”; break; case ‘B’: // Group B: 21 – 40 echo “Ages 21 to 40”; break; case ‘C’: // Group C: 41 – 60 echo “Ages 41 to 60”; break; case ‘D’: // Group D: 61 and older echo “Ages 61 and older”; break; } ?> 20

21 <?php // Assign a number to be compared $intNumber = 7; switch ($intNumber > 5) { case true: echo “$intNumber is greater than 5”; break; case false: echo “$intNumber is less than or equal to 5”; break; } ?> 21

22 Multiple Cases for the Same Code  In some cases you may find that you have two cases in a switch statement that should prefix the same code. In fact, you could have any number of cases that should be followed by one block of code. 22

23 <?php /* color choices using if statement */ $strColor = ‘Blue’; // Decide if color is warm or cool if ($strColor == ‘Red’ || $strColor == ‘Orange’ || $strColor == ‘Yellow’) { echo “$strColor is a warm color”; } else { echo “$strColor is a cool color”; } ?> 23

24 <?php $strColor = ‘Blue’; switch ($strColor) { case ‘Red’: case ‘Orange’: case ‘Yellow’: echo “$strColor is a warm color”; break; default: echo “$strColor is a cool color”; break; } ?> 24

25 exercise  Program Grade: Input point 0-100 form keyboard, and then decision to grade follow this:  80 – 100= A  70 – 79= B  60 – 69= C  50 – 59= D  0 – 49= F  Use if-elseif-else & switch to programming 25


Download ppt "CONTROL STRUCTURE The if, elseif, and else & switch Statements 1."

Similar presentations


Ads by Google