Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Computer Science

Teacher: Maruf Ahmed


Conditional statements / Selection statements

One of the four basic constructs to develop an algorithm is SELECTION. Selection is covered in
programming with Conditional statements / selection statements.
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

Types of conditional statement:


- There are two types of conditional statements structure that can be used.
- One is IF structure and the other one is CASE structure.

N.B. Any statements within the IF…ENDIF block and CASE…ENDCASE block must be indented by at
least two spaces.

IF structure: 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.
For an IF condition the THEN path is followed if the condition is true and the ELSE path is followed if
the condition is false.
Reason to use IF conditional statement:
- When we need to check a condition that may be very complex
- When we need to check a condition where comparison operators or Boolean operators need 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 (no ELSE part in it)


IF (Condition)
THEN
Statement/s
ENDIF
Example:
INPUT A
IF (A > 10)
THEN
OUTPUT “The value is bigger than 10”
ENDIF

Page 1 of 8
Explanation: In the above example, if the condition evaluates to TRUE, the THEN part will be executed,
However, if the condition evaluates to FALSE then since no statement has been set for the FALSE part,
nothing will be executed for the IF part.

Structure 2: IF…THEN…ELSE…ENDIF (one ELSE part)


IF (Condition)
THEN
statement/s
ELSE
statement/s
ENDIF
Example:
INPUT A
IF (A > 10)
THEN
OUTPUT “The value is bigger than 10”
ELSE
OUTPUT “The value is not bigger than 10”
ENDIF
Explanation: In the above example, if the condition evaluates to TRUE, the THEN part will be executed,
otherwise the ELSE part will be executed. This structure will always execute any one part out of the two.

Structure 3 (Nested IF): (Multiple IF and final ELSE together)


- When one IF is inside another IF, then this is known as nested IF

IF…THEN…ELSE.....IF…THEN…ELSE....IF…THEN......ELSE…(Continued)...ENDIF….ENDIF….
IF (Condition)
THEN
statement/s
ELSE
IF (Condition)
THEN
statement/s
………………………………………….
………………………………………… (There may be more IF)
ELSE (The final ELSE which does not have any more IF)
statement/s
ENDIF
ENDIF
Example:
INPUT A
IF (A > 10)
THEN
OUTPUT “The value is bigger than 10”

Page 2 of 8
ELSE
IF (A < 10)
THEN
OUTPUT “The value is less than 10”
ELSE
OUTPUT “The value is equal to 10”
ENDIF
ENDIF
Explanation: The above example is an example of nested IF where one IF is inside another IF. The
program will first check the first IF (known as outer IF). If the condition evaluates to TRUE then it will
not check any other condition inside the nested IF and come out of the IF block. If the first condition
evaluates to FALSE then it will go inside the ELSE part and check the second IF. This will continue the
same way for other IFs also. If there is a final ELSE which does not have any other IF inside it, then
when no condition evaluates to TRUE, the last ELSE part will be executed.
Every IF that has been opened must be closed with ENDIF one by one. The IF which was opened last
will be closed first. The IF which was opened first will be closed last. Each ENDIF must be in separate
line.
Structure 3 (nested IF but without IF in the last ELSE part) Nested IF without any final ELSE
part:
IF (Condition)
THEN
statement/s
ELSE
IF (Condition)
THEN
statement/s
ENDIF (There is no final ELSE part in it)
ENDIF

Problem: 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”
ELSE
IF (Height < 1.25)
THEN
OUTPUT “Shorter than 1.25 metres”
ENDIF
ENDIF
Explanation: In the above example, the height will be checked first with greater than 1.25. If it is true
then the output message will be displayed and it will come out of the IF block. If the first condition is
FALSE, then it will check the second condition, if it is TRUE, then the output statement for that part will
Page 3 of 8
be displayed. If both the conditions are FALSE i.e. if the height is equal to 1.25, then nothing will be
executed as there is no final ELSE part given.
N.B. You should never give any unit such as metres, inches, dollar sign, percentage sign etc. in the
condition part. Condition will be made with only values or variables without any unit or symbol. You
cannot use any curly braces ({ }) or square brackets ([ ]) in a condition. Only parenthesis ( ) are allowed.

The following logical operators and Boolean operators are used in IF structures for comparison:
Operator Comparison
= Is equal to
> Greater than
< Less than These are known as logical
>= Greater than or equal to operators
<= Less than or equal to
<> Not equal to
() Group to change the order of operation
AND - At least two parts will be present in the
condition. Both parts must be true to
become the result TRUE. If either part is
FALSE then the result of the condition
will be FALSE.
- AND is also used to check a range for the
same variable. For example
IF Num >= 100 AND Num <= 200
THEN
OUTPUT “Within range”
ELSE
OUTPUT “Outside range”
ENDIF
OR - At least two parts will be present in the
condition. If any part evaluates to be true
then the condition result will be true. Only AND, OR, and NOT are
if both parts are FALSE then the result
known as Boolean operators
will be FALSE
OR is also used to check if a value of a
variable is outside a range. For example
IF Num < 100 OR Num > 200
THEN
OUTPUT “Outside range”
ELSE
OUTPUT “Within range”
ENDIF
NOT - Makes the result TRUE as FALSE and
FALSE as TRUE. For example,
IF NOT(Age>=18)
THEN
OUTPUT “Minor”
ELSE
OUTPUT “Adult”
ENDIF

Page 4 of 8
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

Explanation: 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.

Use of Separate IFs:


When there is a situation where the conditions are not connected or not dependent on each other then
separate IFs are needed instead of nested IF.
For example:
INPUT Age
IF Age >= 18
THEN
OUTPUT “The person is an adult”
ELSE
OUTPUT “The person is a child”
ENDIF
INPUT Height
IF Height >= 1.0 AND Height <=2.0
THEN
OUTPUT “The person is eligible for the ride”
ENDIF

Explanation: The first condition will be checked and it will give the output message which is given in
the THEN part only if the condition is TRUE. Otherwise it will give the output message that is in the
ELSE part. The second condition will be checked no matter what the result of the first condition is. If the
second condition is TRUE only then the output message will be displayed otherwise no message will be
displayed because no ELSE part has been covered here.

Example pseudocode in which one condition is dependent on the result of another condition

DECLARE Age : INTEGER


DECLARE Weight : REAL

OUTPUT “Enter your age: ”


INPUT Age
IF Age >= 18 AND Age <=60

Page 5 of 8
THEN
OUTPUT “Your age is within the allowed range”
OUTPUT “Enter your weight: ”
INPUT Weight
IF Weight >= 30.5 AND Weight <=100.0
THEN
OUTPUT “You are eligible for the ride”
ELSE
OUTPUT “You are not eligible for the ride”
ENDIF
ELSE
OUTPUT “Your age is outside the allowed range. You cannot avail this ride”
ENDIF

Explanation: First the input of age will be taken. If the result of the condition is TRUE only then it will
go inside the IF and display an output message “Your age is within the allowed range”. If the condition is
FALSE then the ELSE part of the condition will be executed which displays a message “Your age is
outside the allowed range. You cannot avail this ride”. The program ends after that. Only if the first
condition evaluates to TRUE then the weight of the person will be taken as input. If the result of the
condition of the weight is TRUE then the output message “You are eligible for the ride” will be displayed
otherwise “You are not eligible for the ride” will be displayed. So for the second condition to be checked,
the first condition needs to be TRUE.

CASE conditional statement:


N.B. In O‟ Level 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 or not) 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.

Note that the case clauses are tested in sequence. When a case that applies is found, its statement is
executed, and the CASE statement is complete. Control is passed to the statement after the ENDCASE.
Any remaining cases are not tested.

Reason to use CASE conditional statement:


- When a list of options to choose from and the options are discrete (distinct)
- When one out of several branches of code to be executed, depending on the value of a variable

Page 6 of 8
Structure of CASE OF……ENDCASE (no OTHERWISE part in it)
CASE OF variable name
Option1: statemenet/s
Option2: statemenet/s
....................................
....................................
ENDCASE

Structure of CASE OF…….OTHERWISE……ENDCASE (OTHERWISE part present in it)


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 Grade is A, B, or C. That means
we are assuming that single letter will be given as input. For single letters to be taken as input we have to
give the options in single or double quotation mark and the variable must be CHAR.

DECLARE Grade : CHAR


OUTPUT “Enter the grade (A, B or C): ”
INPUT Grade
CASE OF Grade
„A‟ : OUTPUT “Very Good”
„B‟ : OUTPUT “Good”
„C‟ : OUTPUT “Satisfactory”
ENDCASE [No OTHERWISE part has been used in this structure]

Explanation: The user is supposed to give a value from A to C. If A is given then it will display Very
Good and it will come out of the CASE block. No more checking will be done. If B is given, then the
statement associated with B will be executed and then no further checking will be done. It will continue
the same way for other parts also. If an input outside A to C is given then nothing of the CASE block will
be executed as there is no otherwise part given.

Example 2: The algorithm below specifies what happens if the value of Choice is 1, 2 or 3

OUTPUT “Enter your choice from 1 to 3”


INPUT Choice
CASE OF Choice
1 : OUTPUT “Sunday”
2 : OUTPUT “Monday”
3 : OUTPUT “Tuesday”

Page 7 of 8
OTHERWISE OUTPUT “Wrong choice, enter a valid entry”
ENDCASE

Explanation: The user is supposed to give a value from 1 to 3. If option 1 is given then it will display
Sunday and it will come out of the CASE block. No more checking will be done. If option 2 is given,
then the statement associated with 2 will be executed and then no further checking will be done. It will
continue the same way for other options also. If an input outside 1 to 3 is given then the otherwise part
will be executed.

Example 3: Combination of IF and CASE structure together

Result ← 0
OUPTUT “Enter your choice from 1 to 3: ”
INPUT Option
IF Option >=1 AND Option <=3
THEN
OUTPUT “Enter two numbers for calculation: ”
INPUT Num1, Num2
OUTPUT “Select 1 for addition, 2 for Subtraction and 3 for Multiplication”
CASE OF Option
1: Result ← Num1 + Num2
2: Result ← Num1 - Num2
3: Result ← Num1 * Num2
ENDCASE
ELSE
OUTPUT “Wrong entry”
ENDIF
OUTPUT Result

Explanation: An input for option will be taken first which should be between 1 and 3 inclusive. If the
option is given within the range then two numbers for calculation will be taken as input. Depending on
the choice of options any one out of the three that has been set up in the CASE structure will be
performed. If the option is outside the range from 1 to 3 then the message “Wrong entry” will be
displayed. To perform the CASE block, the options must be from 1 to 3 inclusive. When it will come out
of the IF block then the result of the calculation will be displayed. If the option is not between 1 and 3
then the result will be displayed 0.

Page 8 of 8

You might also like