You are on page 1of 19

IT ELEC1

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.

 Kinds of conditions in a program are:


 Simple Condition
 Compound Condition
 Complex Condition

 Simple condition is a kind of condition that uses only one


Conditions comparison or logical operator.

 Compound condition is a kind of condition that is composed of


two simple conditions compared to each other using a logical
operator.

 Complex condition is a kind of condition that is composed of


more than two simple conditions compared to each other using
logical operators.
 Any algorithm or program can be more clear and understood if
they use self-contained modules called control structures.

 Control Structures are just a way to specify flow of control in


programs.

 The term flow of control means details the direction that the
Control program takes.

Structures  Control structures are blocks of programming codes that


analyses variables and chooses a direction in which to go based
on given parameters or conditions.

 Control structures are specific commands in programming


languages which are used to control the logical flow of the
program during its execution.
 Types of Control Structures:
 Sequence Structure
 Selection Structure
 Repetition Structure

 In a sequence structure, an action, or event, leads to the next


ordered action in a predetermined order.

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.

 Selection structure involves a statement or group of statements


which is executed based on the result of a condition.
Control  After either the true set of statements or the false set of
Structures statements are taken, program control resumes with the next
statement following the selection structure.
continue…  Selection structures in PHP:
 if Statement
 if…else Statement
 if…elseif…else Statement
 switch Statement
 A repetition structure is used when a program needs to
repeatedly process one or more statements until some condition
is met at which time the loop ends.

 The decision whether to repeat the statement is based on the


evaluation of a Boolean expression.

Control  A Boolean expression is a logical statement that the result is


either TRUE or FALSE.
Structures  Boolean expressions can compare data of any type as long as
continue… both parts of the expression have the same basic data type.

 Repetition structures in PHP:


 while Loop
 do…while Loop
 for Loop
 foreach Loop
 The if statement executes some code if one condition is true.

 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.

 Use the switch statement to select one of many blocks of code to be


executed.

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

PHP echo “You are a fourth year student.”;


break;
case 5:
echo “You are a fifth year student.”;
break;
default:
echo “Your year level is unknown.”;
}
 Problem:
Write a PHP script that shows, if num1 is not equal to 0 then compute
the power of num1 raised to fifth exponent. It will also display the

Exercise 1 value of num1 and power using separate echo statements.

 Write your answer on your notebook and compare it during next


discussion.
<?php
if ( $num1 <> 0 ) {

Answer: $power = $num1 ** 5;


echo “Value of num1 is ” . $num1 . “.<br>”;

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.

 Write your answer on your notebook and compare it during next


discussion.
<?ph
if ($employee_status > 2) {
$bonus = 10000;

Answer: } else {
echo “Bonus is ” . $bonus . “<br>.”;

Exercise 2 $bonus = 5000;


echo “Bonus is ” . $bonus . “<br>.”;
}
?>
 Problem:
Write a PHP script that shows, if salary is greater than zero and less
than 10,001 then assign 1 to status and display the text, “You
are a job-order employee.”. Otherwise, if salary is greater than
10,000 and less than 20,001 then assign 2 to status and display the
Exercise 3 text, “You are a probationary employee”. Otherwise, assign 3 to
status and display the text, “You are a regular employee”.

 Write your answer on your notebook and compare it during next


discussion.
<?php
if ($salary > 0) And ($salary < 10001) {
$status = 1;
echo “You are a job-order employee.”;
} elseif ($salary > 10000) And ($salary < 20001) {
Answer: $status = 2;
echo “You are a probationary employee.”;
Exercise 3 } else {
$status = 3;
echo “You are a regular employee.”;
}
?>
 Problem:
Write a PHP script that will evaluate the value of year_level and
assign the corresponding value for miscellaneous based on the
year_level on the table below:

Exercise 4

 Write your answer on your notebook and compare it during next


discussion.
<?php
switch ($year_level) {
case 1: $miscellaneous = 3000.00;
break;
case 2: $miscellaneous = 3500.00;
break;

Answer: case 3: $miscellaneous = 4000.00;


break;

Exercise 4 case 4: $miscellaneous = 4500.00;


break;
case 5: $miscellaneous = 5000.00;
break;
default: $miscellaneous = 0;
}
?>

You might also like