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

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

Embed Size (px)

Citation preview

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

1

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

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

example if Conditionals

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

4

If-else Conditionals

The if statement follows this syntax:if (condition){

Statements is true}else{

Statements is false}

5

example if-else Conditionals

<?php$count=5;// Condition is trueif ($count==10){echo “Condition 1 outputs this TRUE.”;}// Condition is false{echo “Condition 2 outputs this FALSE.”;}?>

6

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

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

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

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

1

0

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.

1

1

the Logical Operators

The && operator returns true if and only if both operands are true.

Operator Name ExampleAND And $var1 and $var2OR Or $var1 or $var2XOR Exclusive or $var1 xor

$var2&& And $var1 && $var2|| Or $var1 ||! Not !$var1

1

2

AND operator in detail.

The AND (or &&) Operator Requires That Both of the Values Be True

Left Value Right Value Return ValueTrue True TrueTrue False FalseFalse True FalseFalse False False

1

3

OR operator in detail.

The OR (or ||) Operator Requires That at Least One of the Values Be TrueLeft Value Right Value

Return ValueTrue True TrueTrue False TrueFalse True TrueFalse False False

1

4

XOR operator in detail.

The XOR Operator Requires That Only One of the Values Be True.

Left Value Right Value Return Value

True True FalseTrue False TrueFalse True TrueFalse False False

1

5

Not operator in detail.

The NOT (or !) Operator Reverses the Value of a Boolean Value

Right Value Return ValueTrue FalseFalse True

1

6

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

1

7

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;

}

1

8

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”; }

?>

1

9

<?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;

}

?>

2

0

<?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;

}

?>

2

1

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.

2

2

<?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”;

}

?>

2

3

<?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;

}

?>

2

4

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

2

5