10
Lecture 2 Conditional Statement

Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

Embed Size (px)

Citation preview

Page 1: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

Lecture 2

Conditional Statement

Page 2: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

Conditional Statements in PHP

Conditional Statements are used for decision making. Different actions are

performed for different decision. PHP has the following conditional statements

If statement: executes a code only if a condition is met.If – else statement: executes a code if a condition is true

and if false, executes another code.If – else if – else statement: if the first condition is not

true, another condition is tested, if all condition fails, then a code is executed.Switch Statement: selects a block of code out of several

blocks to be executed.

Page 3: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

IF Statement

If statement executes a code only if a condition is met. It has the syntax:

If (condition){do something}For example <?php$image = “car”;If ($image == “car”){print ‘<img src = “images/work. jpg” alt = “a red car”

height = “150” width =“150” >’;}?>

Page 4: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

So here we have used both the if statement and the comparison

operator = = (equal) . It checks if the value assigned to the variable

$image is car, if it is, the image is displayed and if not, nothing is

displayed.Assuming we change the value of $image to

be moon. Nothing will be displayed because the condition is false.

Page 5: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

If-else statement

Assuming we want to do something else if a condition is false, the if-else statement is used.

It has the syntaxIf (condition){do something}else{Do something else}

For example, lets try using another comparison operator.

Page 6: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

practical 5.2Given x = 5, write a program that will check if the

value of x is an odd number or an even number.To do this, we will first declare a variable x and then

store the value 5 in it. Then we will write the if – else statement. We also know that every

even number is divisible by 2 without remainder and every odd number will have a

remainder when divided by 2. to test this condition, we will use the modulus operator to check if

dividing the value of x by 2 gives a remainder or not then a comparison operator to check.

Page 7: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

solution<?php$x = 5; if ($x%2 !=0){print “X is an odd number”;}else{print “X is an even number”;}

Page 8: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

If-else if -elseAssuming we have more than two conditions to test.

The if – else if –else statement is used. It has the syntaxif (condition){Do something;}else if (another condition){Do something;}else{Do something else;}

Page 9: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

Switch statementAssuming we have more than two conditions to test, multiple if

statement will be more complex to handle this. The best way will be to use a switch statement. It chooses one block of

code out of several blocks to execute if the condition for that code evaluates to true. It

has the syntax.Switch (n){case x:Do something;Break;Case x:Do something;Break;Default:Do something;}Where n is the variable name and x is the value of the variable we are

testing.The break is used to prevent the code from automatically running into the next

block of codeThe default is used to handle a situation when all case fails or evaluates to false.

Page 10: Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions

chcslonline.org

practical 5.3Write a program to output the class of a grade depending on

the Grade of the user.<?php$grade =“A” ;Switch ($grade){Case “A”:echo “Excellent”;break;Case “B”:Echo “very good”;Break;Case “C”:Echo “Average”;Break;Default:Echo “Please Enter a valid grade”;}