Conditional Statements
Conditional Statements
One of the 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
N.B. Any statements within the IF…ENDIF block and CASE…ENDCASE block must be indented by at
least four 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 logical operators or Boolean operators need to be used
The IF conditional statements can be represented in three different ways. The structure and example for each
one is given below:
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”
ELSE
IF (A < 10)
THEN
OUTPUT “The value is less than 10”
ELSE
OUTPUT “The value is equal to 10”
Page 2 of 9
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 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.
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
Page 4 of 9
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.
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
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.
For example,
DECLARE Found : BOOLEAN
Found ← TRUE
IF Found //This is same as IF Found = TRUE. This can be written in either way
THEN
OUTPUT “The value has been found”
ELSE
OUTPUT “The value has not been found”
ENDIF
Another example,
DECLARE Found : BOOLEAN
Found ← FALSE
IF NOT Found //This is same as IF Found = FALSE. This can be written in either way
THEN
OUTPUT “The value has not been found”
ELSE
OUTPUT “The value has been found”
ENDIF
Explanation: When a condition is given with a variable name only, then it must be a Boolean type variable.
Because no other data type can be used in a condition with the variable name only. If in a condition the
name of the variable is given only then the condition is actually checking if the value assigned with the
variable is TRUE or not. If previously the variable value has been assigned with TRUE, then the condition
evaluates as TRUE otherwise FALSE. As a result, the statement/s will be executed for the TRUE part when
it evaluates to TRUE and the statement/s will be executed for the FALSE part when it evaluates as FALSE.
Page 6 of 9
On the other hand, if the condition is set with NOT followed by the variable name, then it is checking for the
variable value with FALSE. As a result, the statement/s will be executed for the TRUE part when it
evaluates as TRUE and the statement/s will be executed for the FALSE part when it evaluates as FALSE.
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.
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.
Page 7 of 9
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
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.
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
Page 8 of 9
ENDCASE
ELSE //ELSE part for the first IF
OUTPUT “Wrong choice”
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 9 of 9