Selection Constructs (SQA National 5 Computing Science): Revision Note
Exam code: X816 75
Selection constructs
What is a selection construct?
Selection constructs (often referred to as if statements) are fundamental components of programming, allowing a computer program to make decisions based on some input
Students are required to describe, exemplify, and implement these constructs in a high-level (textual) language
Simple conditional statements
Simple conditional statements rely on logical comparators to compare two values
Students must be able to use and implement the following operators
Operator | SQA pseudocode | Python equivalent | Description |
|---|---|---|---|
Equal to |
|
| Checks if two values are the same |
Not equal to |
|
| Checks if two values are different |
Less than |
|
| Checks if the first value is smaller than the second |
Less than or equal to |
|
| Checks if the first value is smaller than or equal to the second |
Greater than |
|
| Checks if the first value is larger than the second |
Greater than or equal to |
|
| Checks if the first value is larger than or equal to the second |
A basic structure uses
IF...THEN...ELSE...END IFto execute different code blocks depending on whether the condition is metIndentation is used in pseudocode to clearly show the selection structure
SEND "Enter your age:" TO DISPLAY
RECEIVE age FROM KEYBOARD
IF age < 12 THEN
SEND "You are a child." TO DISPLAY
ELSE
IF age < 18 THEN
SEND "You are a teenager." TO DISPLAY
ELSE
IF age ≥ 18 THEN
SEND "You are an adult." TO DISPLAY
END IF
END IF
END IF
SEND "Enter your favourite colour:" TO DISPLAY
RECEIVE colour FROM KEYBOARD
IF colour = "blue" THEN
SEND "You like the colour blue!" TO DISPLAY
END IF
IF colour ≠ "blue" THEN
SEND "You like a different colour!" TO DISPLAY
END IFAn example written in Python would be
age = int(input("Enter your age: "))
if age < 12:
print("You are a child.")
elif age < 18:
print("You are a teenager.")
elif age >= 18:
print("You are an adult.")
colour = input("Enter your favourite colour: ")
if colour == "blue":
print("You like the colour blue!")
if colour != "blue":
print("You like a different colour!")Notice Python does not use
THENandEND IFELIFcan be used to extend a selection statement (ELSE IF)
Complex conditional statements
Complex conditional statements combine multiple simple conditions using logical operators
Students must understand and implement the three main logical operators:
AND,OR, andNOT
Operator | When the result is TRUE | Function |
|---|---|---|
| Both parts of the condition are true | Used when all criteria must be met |
| At least one part of the condition is true | Used when any of the criteria can be met |
| Flips the value (e.g., True becomes False) | Used to negate a condition |
Example
SQA Pseudocode | Python |
|---|---|
| |
Efficiency and logic
Efficiency in complex conditions
Using a single complex conditional statement is often more efficient than nesting multiple simple conditional statements
Less Efficient (Nested IFs): Two separate simple conditional statements nested together.
More Efficient (Complex IF):
IF X > 4 AND Y < 6 THEN....
Efficiency in multi-way selection
When checking multiple alternatives (e.g., assigning a grade based on a score), using nested IF constructs or equivalents like ELSE IF/ELIF statements is generally more efficient than using multiple, separate
IFconstructsWhen separate
IFconstructs are used, the program always carries out all comparisonsUsing nested structures means the program stops once a condition is met, potentially requiring fewer comparisons
Potential logic errors
If logical operators are used incorrectly, the program will suffer a logic error, meaning the code produces an unexpected result
A common example is an incorrect range check, such as using AND where OR is required (e.g., checking if
$number < 30$ AND $number > 50$), which can never be true, resulting in a logic error
Worked Example
A program is designed to classify a customer's monthly data usage (in Gigabytes, GB) and set a corresponding pricing tier:
Data Usage (D) | Tier |
|---|---|
D<50 GB | Basic |
50≤D≤150 GB | Standard |
D>150 GB | Premium |
The programmer implemented the following code using separate selection constructs:
Line 10 SET dataUsage TO 75
Line 11 IF dataUsage < 50 THEN
Line 12 SET pricingTier TO "Basic"
Line 13 END IF
Line 14 IF dataUsage >= 50 AND dataUsage <= 150 THEN
Line 15 SET pricingTier TO "Standard"
Line 16 END IF
Line 17 IF dataUsage > 150 THEN
Line 18 SET pricingTier TO "Premium"
Line 19 END IF
Line 20 SEND pricingTier TO DISPLAY(a) Explain why the selection structure shown in lines 11 to 19 is considered inefficient.
[2]
(b) Rewrite lines 11 to 19 of the code to make the selection structure efficient using a programming language of your choice. You must ensure the correct logical outcome is still produced.
[2]
Answers
(a)
The program uses three separate
IFstatements [1 mark]The use of multiple separate
IFstatements is identified as less efficient than nested structures
All three conditions (lines 11, 14, and 17) must be checked every time the code runs [1 mark]
This is the core reason for inefficiency: a nested structure would stop checking once a true condition is found, whereas separate IFs require all comparisons to be carried out
(b)
Line 11 IF dataUsage < 50 THEN [1 mark for correct structure (IF, ELSE IF, ELSE)]
Line 12 SET pricingTier TO "Basic"
Line 13 ELSE IF dataUsage <= 150 THEN [1 mark for simplified and correct remaining condition]
Line 14 SET pricingTier TO "Standard"
Line 15 ELSE
Line 16 SET pricingTier TO "Premium"
Line 17 END IFExaminer Tips and Tricks
Prioritise efficiency with nested structures
For multi-way decisions (where only one outcome is possible, such as determining a pricing tier or grade), always use nested IF constructs or ELSE IF statements
Using multiple separate
IFconstructs one after the other is considered inefficient, as the program must check every condition, rather than exiting once a condition is satisfied
Unlock more, it's free!
Did this page help you?