Conditional Statements q Very often when you write code, you want to perform different actions for different decisions: v If-statement v If-elseif-else statement
Conditional Statements q #Example <? php $t = date(“H”); if ($t< “ 10”) { echo”Have a good morning”; } else if ($t < “ 20”) { echo”Have a good day”; } else { echo”Have a good night”; } ? >
Conditional Statements q Selects one of many blocks of code to be executed <? php $favcolor = “red”; switch ($favcolor) { case “red”: echo”Your favourite color is red!”; break; case”green”: echo” Your favourite color is green!”; break; default: echo”Your favourite color is missing”; } ? >
Loops q Often you want write code, you want the same block of code to run over and over again in a row. v While v Do. . while v Foreach (Array)
Loops q #Example 1 <? php $x = 1; while ($x <=5) { echo “Number of x is : $x ”; $x++; } ? >
Loops q #Example 2 <? php $x = 1; do { echo “Your number is : $x ”; } while ($x <=5) ? >
Loops q #Example 3 <? php for ($x = 0; $x <=10; $x++) { echo”The number is : $x ”; } ? >
Exercise q #Exercise 1 (write code using IF-ELSE statement) Mark Gred 80 – 100 A 60 - 79 B Below 79 C
Exercise q #Exercise 2 (write code using WHILE statement) Num Operation 5 100 + 10 4 + 10 3 + 10 2 + 10 1 + 10
Exercise q #Exercise 3 (write code using FOR statement) Num Total, R Status 1 1000 + 200 If R more than 1300, display status equal to “High”, else status equal to “Low” 2 + 200 If R more than 1300, display status equal to “High”, else status equal to “Low” 3 + 200 If R more than 1300, display status equal to “High”, else status equal to “Low”