Professional Documents
Culture Documents
Midterm Discussion 1
Conditions are statements that are created by the programmer
which evaluates actions in the program and evaluates if it's true
or false.
The term flow of control means details the direction that the
Control program takes.
Control The sequence can contain any number of actions, but no actions
Structures can be skipped in the sequence.
continue… The program, when run, must perform each action in order with
no possibility of skipping an action.
The selection structure allows one set of statements to be
executed if a condition is true and another set of statements to
be executed if a condition is false.
Syntax:
if (condition) {
code to be executed if condition is true;
if Statement in }
PHP Example:
<? php
if ( age > 59 ) {
echo “You are a Senior Citizen.”;
}
?>
The if...else statement executes some code if a condition is true and
another code if that condition is false.
Syntax:
if (condition) {
code to be executed if condition is true;
} else {
if…else }
code to be executed if condition is false;
Statement in Example:
PHP <? php
if ( age > 59 ) {
echo “You are a Senior Citizen.”;
} else {
echo “You are not a Senior Citizen.”;
}
?>
The if...elseif...else statement executes different codes for more than two
conditions.
Syntax:
if (condition1) {
code to be executed if condition1 is true;
} elseif (condition2) {
code to be executed if condition2 is true;
}
…
if…elseif… } else {
code to be executed if all conditions are not tue;
else Statement }
in PHP Example:
<? php
if ( age > 59 ) {
echo “You are a Senior Citizen.”;
} elseif (age < 59) && (age > 17) {
echo “You are an Adult Citizen.”;
} else {
echo “You are a Minor Citizen.”;
}
?>
The switch statement is used to perform different actions based on
different conditions.
Syntax:
switch (n) {
case label1:
switch code to be executed if n=label1;
break;
Statement in case label2:
code to be executed if n=label2;
PHP break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all
labels;
}
Example:
switch (year_level) {
case 1:
echo “You are a first year student.”;
break;
case 2:
echo “You are a second year student.”;
break;
switch case 3:
echo “You are a third year student.”;;
Statement in case 4:
break;
Exercise 1 }
echo “num1 raised to the fifth exponent is ” . $power;
?>
Problem:
Write a PHP script that shows, if employee_status is greater than 2
then it will assign 10,000 to bonus and display it using echo
statement. Otherwise, it will assign 5,000 to bonus and display it using
Exercise 2 echo statement.
Answer: } else {
echo “Bonus is ” . $bonus . “<br>.”;
Exercise 4