Computer Science
Teacher: Maruf Ahmed
Conditional statements / Selection statements
When different actions are performed by an algorithm according to the values of the variables,
conditional statements can be used to decide which action should be taken. The path is decided depending
on the result of the condition. A condition always gives the result as True or False.
The purpose of a conditional statement:
It allows different routes through a program which are dependent on meeting certain criteria
There are two types of conditional statements that can be used. One is IF statement and the other one is
CASE statement.
IF conditional statement: In an IF conditional statement, the result of a condition can be either true or
false. True is when condition is met and False is when condition does not meet.
Reason to use IF conditional statement:
- Checking a condition that may be very complex
- Checking a condition where relational operators can be used
The IF conditional statements can be represented in three different ways. The structure and example for
each one is given below:
Structure 1: IF…..THEN…..ENDIF
IF (Condition)
THEN
Statement/s
ENDIF
Example:
IF (A > 10)
THEN
OUTPUT “The value is bigger than 10”
ENDIF
Structure 2: IF…THEN…ELSE…ENDIF
IF (Condition)
THEN
statement/s
ELSE
statement/s
ENDIF
Example:
IF (A > 10)
THEN
Page 1 of 4
OUTPUT “The value is bigger than 10”
ELSE
OUTPUT “The value is not bigger than 10”
ENDIF
Structure 3 (Nested IF):
IF…THEN…ELSE.....IF…THEN…ELSE....IF…THEN......ELSE…(Continued)...ENDIF….ENDIF….
Example:
IF (A > 10)
THEN
OUTPUT “The value is bigger than 10”
ELSE
IF (A < 10)
THEN
OUTPUT “The value is less than 10”
ELSE
OUTPUT “The value is equal to 10”
ENDIF
ENDIF
The above example is an example of nested IF where one IF is inside another IF. The inner IF will be
completed first and then the outer IF will be completed. There has to be separate ENDIF for each IF that
you have used.
An example where separate IFs are used and not needed to use nested IF. They are not connected
so two different IFs have been used.
The height of a student is measured. Find out if the student is taller than 1.25 metres or shorter than 1.25
metres.
INPUT Height
IF (Height > 1.25)
THEN
OUTPUT “Taller than 1.25 metres” [structure 1 has been followed]
ENDIF
IF (Height < 1.25)
THEN
OUTPUT “Shorter than 1.25 metres” [structure 1 has been followed]
ENDIF
Page 2 of 4
N.B. In the above solution is equal to (=) has not been covered as the problem did not require it. That is
why two different IF has been used. You should never give any unit such as metres, inches etc. in the
condition part. Condition will be made with only values or variables without any unit.
The following comparison operators are used in IF condition:
Operator Comparison
= Is equal to
> Greater than
< Less than These are known as logical /
>= Greater than or equal to conditional operators
<= Less than or equal to
<> Not equal to
() Group to change the order of operation
AND Both parts must be true
OR Either one has to be true These are known as Boolean
NOT Makes the result FALSE as TRUE and operators
TRUE as FALSE
Comparisons are made from left to right, for example A > B means the result will be TRUE only if the
value of A is bigger than that of B. Comparisons can be simple or more complicated. For example,
IF ((Height > 1) OR (Weight > 20) OR (Age > 5)) AND (Age < 70)
THEN
OUTPUT “You can ride”
ELSE
OUTPUT “Too small, too young or too old”
ENDIF
In the above example, if any one part of OR is true then the left part will be true but for the whole
condition to be true the right part of the condition must also evaluate to be true.
CASE conditional statement:
N.B.: In your syllabus, CASE structure has a very limited use. This is only used as a replacement of
nested If statement where value of a variable will be checked for equality (whether something is equal to
something) only. The CASE structure and nested IF may be used alternatively where equality will be
checked.
For a CASE statement, the value of the variable decides the path to be taken. Several values are usually
specified. OTHERWISE is the path taken for all the other values. The end of the statement is shown by
ENDCASE. OTHERWISE may or may not be present in the structure.
It is best practice to keep the branches to single statements as this makes the pseudocode more readable.
Similarly, single values should be used for each case. If the cases are more complex, the use of an IF
statement, rather than a CASE statement, should be considered.
Reason to use CASE conditional statement:
- Usually a list of options to choose from and the options are discrete
Page 3 of 4
Structure of CASE……OF…….OTHERWISE……ENDCASE
CASE OF variable name
Option1: statemenet/s
Option2: statemenet/s
....................................
OTHERWISE statement/s
ENDCASE
Example 1: The algorithm below specifies what happens if the value of Choice is 1, 2 or 3
INPUT Choice
CASE OF Choice
1 : OUTPUT “Sunday”
2 : OUTPUT “Monday”
3 : OUTPUT “Tuesday”
OTHERWISE OUTPUT “Wrong choice, enter a valid entry”
ENDCASE
Example 2: The algorithm below specifies what happens if the value of Grade is A, B, or C. That means
we are assuming that text will be given as input. For text to be taken as input we have to give the options
in single or double quotation mark and the variable must be either a CHAR or STRING.
INPUT Grade
CASE OF Grade
„A‟ : OUTPUT “Very Good”
„B‟ : OUTPUT “Good”
„C‟ : OUTPUT “Satisfactory”
ENDCASE [No OTHERWISE in this structure]
Example 3: Calculating addition, subtraction and multiplication using CASE structure
INPUT Option, Num1, Num2
CASE OF Option
1: Result ← Num1 + Num2
2: Result ← Num1 - Num2
3: Result ← Num1 * Num2
OTHERWISE OUTPUT “Wrong entry”
ENDCASE
Page 4 of 4