Top Banner
Conditional Stataments Conditional Stataments Chapter 3 MOHAMAD RAHIMI MOHAMAD ROSMAN
19
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 03   conditional statements

Conditional StatamentsConditional StatamentsChapter 3

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 2: Chapter 03   conditional statements

IntroductionIntroduction

Very often when you write code, you want to perform different actions for different decisions.

You can use conditional statements in your code to do this.◦ if...else statement

use this statement if you want to execute a set of code when a condition is true and another if the condition is not true

◦ elseif statement is used with the if...else statement to execute a set of

code if one of several condition are true

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 3: Chapter 03   conditional statements

PHP If...Else StatementsPHP If...Else StatementsMOHAMAD RAHIMI MOHAMAD ROSMAN

if (condition) code to be executed if condition is true;else code to be executed if condition is false;

Page 4: Chapter 03   conditional statements

PHP If...Else Statements PHP If...Else Statements

SYNTAX if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 5: Chapter 03   conditional statements

ExampleExample

The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":

MOHAMAD RAHIMI MOHAMAD ROSMAN

<html><body><?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?></body></html>

Page 6: Chapter 03   conditional statements

ExampleExample

If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:

<html><body><?php$d=date("D");if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; }else echo "Have a nice day!"; ?></body></html>

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 7: Chapter 03   conditional statements

The ElseIf StatementThe ElseIf Statement

If you want to execute some code if one of several conditions are true use the elseif statement

BASIC SYNTAX if (condition) { code to be executed if condition is true; } elseif (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 8: Chapter 03   conditional statements

ExampleExample

The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":

MOHAMAD RAHIMI MOHAMAD ROSMAN

<html><body>

<?php$d=date("D");

if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?></body></html>

Page 9: Chapter 03   conditional statements

Switch statementSwitch statement

The Switch statement in PHP is used to perform one of several different actions based on one of several different conditions.

If you want to select one of many blocks of code to be executed, use the Switch statement.

The switch statement is used to avoid long blocks of if..elseif..else code.

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 10: Chapter 03   conditional statements

Basic SyntaxBasic SyntaxMOHAMAD RAHIMI MOHAMAD ROSMAN

switch (expression){case label1: code to be executed if expression = label1; break;

case label2: code to be executed if expression = label2; break;

default: code to be executed if expression is different from both label1 and label2;}

Page 11: Chapter 03   conditional statements

How it worksHow it works

A single expression (most often a variable) is evaluated once

The value of the expression is compared with the values for each case in the structure

If there is a match, the code associated with that case is executed

After a code is executed, break is used to stop the code from running into the next case

The default statement is used if none of the cases are true

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 12: Chapter 03   conditional statements

ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN

<html><body><?php$x=1;switch ($x){case 1: echo "Number 1"; break;case 2: echo "Number 2"; break;case 3: echo "Number 3"; break;default: echo "No number between 1 and 3";}?></body></html>

Page 13: Chapter 03   conditional statements

ExerciseExerciseMOHAMAD RAHIMI MOHAMAD ROSMAN

Write a program that corresponds to the flowchart below

N=7

M=10

Page 14: Chapter 03   conditional statements

ExerciseExerciseItemsItems PricePrice

CoffeeCoffee 1.001.00

TeaTea 0.800.80

CappuccinoCappuccino 5.005.00

OrangeOrange 2.502.50

MOHAMAD RAHIMI MOHAMAD ROSMAN

Create a program using Switch statement.

x=“Tea”

1. Displays the name of the items

2. Display the price of the items

3. Display (item prices x 15)

Page 15: Chapter 03   conditional statements

Write a program that corresponds to the flowchart below

Test the program using above data◦ intTemp=30◦ intTemp=50◦ intTemp=88

MOHAMAD RAHIMI MOHAMAD ROSMAN

Is intTemp > 32

Is intTemp > 80

Weather ="Freezing"

Weather = "Hot"

Weather ="Moderate"

True

False

No

Yes

DisplayWeather

Page 16: Chapter 03   conditional statements

Odd & Even NumberOdd & Even NumberMOHAMAD RAHIMI MOHAMAD ROSMAN

Create a program to determine whether the number is an odd or even.

Test your coding by using the following data:

Data Result

Set x=30

Set x=13

Set x=7

Page 17: Chapter 03   conditional statements

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 18: Chapter 03   conditional statements

ExerciseExerciseCreated a php statement that request mark from user and display the grade for the mark. The input must not be greater than 100 and must be greater or equal to 0

MOHAMAD RAHIMI MOHAMAD ROSMAN

Gred Marks

90-100 A+

80-89 A

75-79 A-

70-74 B+

65-69 B

60-64 B-

55-59 C

Page 19: Chapter 03   conditional statements

exerCiseexerCise

Create a script that request visitors IC number, then determine the gender of the visitor by displaying it through the alert box.

MOHAMAD RAHIMI MOHAMAD ROSMAN