You are on page 1of 12

Algorithm

Development
Conditional Statements
Conditional Statements
Conditional statements allow the
computer to compare variables
and select one of two
alternatives or decide whether to
repeat an instruction or set of
instructions.
Types of Conditional
Statement
There are two main types of Conditional Statements:
1. Selection: the presentation of a condition, and the
choice between two or more actions, depending on
whether the condition is true or false.

2. Repetition: the presentation of a set of


instructions to be performed repeatedly, as long as
a condition is true.
OPERATORS
Operators are used in expressions to
represent the operations that should
be performed on given operand. The
three basic ones that are normally
used in programming are:
 ARITHMETIC

 RELATIONAL

 LOGICAL
ARITHMETIC OPERATORS

DIV DIV Sum = Sum Div 3 4 1


RELATIONAL (COMPARISON)
OPERATORS
An operator that compares two values. MUST be
present in all conditional statement.

For example, the expression x < 5

means x is less than 5. This expression will have a


value of TRUE if the variable x is less than 5;
otherwise the value of the expression will be FALSE.

Relational operators are sometimes called


comparison operators. Expressions that contain
relational operators are called relational
expressions.
RELATIONAL (COMPARISON)
OPERATORS
Symbol Meaning
= Equal to
=! OR = OR <> Not Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Conditional Statements
 Syntax
<identifier> <relational operator> <identifier>
e.g. (grade > passmark)
<identifier> <relational operator> <literal>
e.g. (grade >= 60)
<identifier> <relational operator> <string>
e.g. (gender = “Male”)
Class Work
Write an algorithm to prompt for and accept
the grade of a student. If the grade is greater
than 59% output “PASS” otherwise output
“FAIL”
Solution:
START
Write “Please enter grade”
Read grade
IF grade > 59 THEN
Write “PASS”
ELSE
Write “FAIL”
ENDIF
STOP
START

Print “Enter
grade”

Read grade

F Print
Grade >
59 “Fail”

T
Print
“Pass”

STOP
Class Work
Write an algorithm to prompt for and accept the
cost of an item. If the cost is greater than
$200 the customer is entitled to a 20%
discount. Output the cost
Class Work
Write an algorithm to prompt for and accept the cost of
an item. If the cost is greater than $200 the customer
is entitled to a 20% discount. Output the cost
Solution:
START
Var cost, NewPrice, Discount
Print “The cost of an item”
Read cost
If cost > 200 THEN
Discount = cost * 0.2
NewPrice = cost – Discount
Print “The cost of the item is “, NewPrice
Else
Write “The cost of the item is “, Cost
End If
STOP

You might also like