0% found this document useful (0 votes)
4 views9 pages

Conditional Statements

The document provides an overview of conditional statements, specifically focusing on selection statements in programming, which allow algorithms to perform different actions based on variable values. It details the types of conditional statements, including IF and CASE structures, and explains their syntax, usage, and examples. Additionally, it covers logical and Boolean operators, the importance of indentation, and best practices for using these constructs in pseudocode.

Uploaded by

pharhomes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Conditional Statements

The document provides an overview of conditional statements, specifically focusing on selection statements in programming, which allow algorithms to perform different actions based on variable values. It details the types of conditional statements, including IF and CASE structures, and explains their syntax, usage, and examples. Additionally, it covers logical and Boolean operators, the importance of indentation, and best practices for using these constructs in pseudocode.

Uploaded by

pharhomes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Computer Science

Teacher: Maruf Ahmed


Conditional statements / Selection 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

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

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
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.
Page 1 of 9
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”
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.

Use of different operators in a condition:


Logical operators: Greater than (>), Less than (<), Greater than or equal to (>=), Less than or equal to (<=),
Is equal to (=) and Not equal to (< >) are known as logical operators
Page 3 of 9
Boolean operators: NOT, AND and OR and are known as Boolean operators
The following logical operators and Boolean operators are used in IF structures for comparison:
Operator Comparison
= Is equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
() Parenthesis, used to change the order of operation. No other brackets can be used
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 if both parts are FALSE then
the result 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

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.

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
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 //This ELSE part is for the first IF part
OUTPUT “Your age is outside the allowed range. You cannot avail this ride”
Page 5 of 9
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.

Use of Boolean data type variable:


- Boolean variables can be used for checking a value whether it is equal to TRUE or FALSE. No other
type of checking can be done with Boolean variables
- Only Boolean variables can be used to compare directly with the variable name without giving any
value in the condition
- For example, a Boolean variable IsPrime has been assigned as TRUE. Later on this can be used in a
conditional statement as IF IsPrime which would check if the value of IsPrime is TRUE or IF NOT
IsPrime which checks if the value of IsPrime is FALSE

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.

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 example, whether the value input is 1, 2, or 3 etc.

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

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


CASE OF variable name
Option1: statement/s
Option2: statement/s
....................................
....................................
ENDCASE

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


CASE OF variable name
Option1: statement/s
Option2: statement/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.

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

OUTPUT “Enter your choice from 1 to 3”


INPUT Choice
CASE OF Choice
1 : OUTPUT “Sunday”
2 : OUTPUT “Monday”
3 : OUTPUT “Tuesday”
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
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.

Use of IF and CASE structure in flowchart:


• There is a decision box that has to be used for each IF or CASE structure in flowcharts.

Page 9 of 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy