Selection Constructs (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

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 IF to execute different code blocks depending on whether the condition is met

  • Indentation 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 IF
  • An 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 THEN and END IF

  • ELIF can 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, and NOT

Operator

When the result is TRUE

Function

AND

Both parts of the condition are true

Used when all criteria must be met

OR

At least one part of the condition is true

Used when any of the criteria can be met

NOT

Flips the value (e.g., True becomes False)

Used to negate a condition

Example

SQA Pseudocode

Python

SEND "Enter your test score:" TO DISPLAY
RECEIVE score FROM KEYBOARD

IF score >= 70 THEN
    SEND "Grade: A" TO DISPLAY
ELSE
    IF score >= 60 AND score < 70 THEN
        SEND "Grade: B" TO DISPLAY
    ELSE
        IF score >= 50 AND score < 60 THEN
            SEND "Grade: C" TO DISPLAY
        ELSE
            IF score < 50 THEN
                SEND "Fail" TO DISPLAY
            END IF
        END IF
    END IF
END IF

SEND "Enter your resit status (yes/no):" TO DISPLAY
RECEIVE resit FROM KEYBOARD

IF resit ≠ "yes" THEN
    SEND "Good luck with your next course!" TO DISPLAY
ELSE
    SEND "You will be entered for a resit." TO DISPLAY
END IF
score = int(input("Enter your test score: "))

if score >= 70:
    print("Grade: A")
elif score >= 60 and score < 70:
    print("Grade: B")
elif score >= 50 and score < 60:
    print("Grade: C")
elif score < 50:
    print("Fail")

resit = input("Enter your resit status (yes/no): ")

if resit != "yes":
    print("Good luck with your next course!")
else:
    print("You will be entered for a resit.")

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 IF constructs

  • When separate IF constructs are used, the program always carries out all comparisons

  • Using 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 IF statements [1 mark]

    • The use of multiple separate IF statements 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 IF

Examiner 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 IF constructs 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!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.