[8.1.4b] Selection

[8.1.4b] Selection

Selection is a control structure that lets a program choose between alternative paths based on a condition. In everyday life you already use selection: if it is raining, you take a coat; otherwise, you do not. In programming, selection is written using IF statements or CASE statements. Mastering selection helps you design algorithms that respond to different inputs, handle special cases, and produce correct outputs for all situations.

What a condition is

A condition is a logical test that evaluates to TRUE or FALSE. Conditions often compare values using operators such as <, <=, >, >=, =, and <> (not equal). You can combine conditions with AND, OR, and NOT. The program follows one branch if the condition is TRUE and a different branch if it is FALSE. Because conditions usually depend on user input or sensor readings, thinking carefully about boundaries and categories is essential.

IF family: one-way, two-way, and multi-way

These examples demonstrate typical IF patterns. Compare how each shape reads and what bugs are likely if you choose the wrong one.

One-way IF: perform an optional step when a rule is met

Use when you either do something or do nothing. Example: add a free gift only if the spend is at least £50.

// CAIE Pseudocode
DECLARE spend : REAL ← 62.50
IF spend >= 50 THEN
  OUTPUT "Free gift added."
ENDIF

# Python
spend = 62.50
if spend >= 50:
    print("Free gift added.")

If the condition is FALSE, the program simply continues with the next statement. No ELSE is needed.

IF...ELSE: exactly one of two branches

Use when there are two mutually exclusive outcomes: pass or fail, in-range or out-of-range, allowed or denied.

// CAIE Pseudocode
DECLARE age : INTEGER ← 15
IF age >= 16 THEN
  OUTPUT "Eligible for a part-time job."
ELSE
  OUTPUT "Not eligible yet."
ENDIF

# Python
age = 15
if age >= 16:
    print("Eligible for a part-time job.")
else:
    print("Not eligible yet.")

Exactly one branch runs. Use clear and non-overlapping conditions so the ELSE truly represents “otherwise”.

IF...ELSEIF ladder: multiple ordered tests

Use when you have several tests that should be checked in order. As soon as one test is TRUE, the rest are skipped.

// CAIE Pseudocode (grade bands)
DECLARE mark : INTEGER ← 72
IF mark >= 85 THEN
  OUTPUT "Grade A*"
ELSEIF mark >= 70 THEN
  OUTPUT "Grade A"
ELSEIF mark >= 60 THEN
  OUTPUT "Grade B"
ELSE
  OUTPUT "Below B"
ENDIF

# Python
mark = 72
if mark >= 85:
    print("Grade A*")
elif mark >= 70:
    print("Grade A")
elif mark >= 60:
    print("Grade B")
else:
    print("Below B")

Order matters. Place the most specific or highest threshold first so earlier branches do not swallow later ones.

CASE statements (also called switch)

A CASE statement provides a tidy way to choose between several discrete, named options of the same variable. It improves readability when you would otherwise chain many ELSEIF tests comparing the same value. A CASE normally handles exact matches and an OTHERWISE (default) branch.

Discrete options with CASE

Good for menus or modes where the input should be one of a small set of values.

// CAIE Pseudocode
DECLARE choice : CHAR ← 'B'
CASE OF choice
  'A': OUTPUT "Add new record"
  'B': OUTPUT "Browse records"
  'C': OUTPUT "Create report"
  OTHERWISE OUTPUT "Invalid option"
ENDCASE

# Python (match available in recent versions; elif shown for portability)
choice = 'B'
if choice == 'A':
    print("Add new record")
elif choice == 'B':
    print("Browse records")
elif choice == 'C':
    print("Create report")
else:
    print("Invalid option")
IF ladder vs CASE (comparison)
AspectIF..ELSEIFCASE
Readability with many fixed valuesCan become long and repetitiveCompact and clearer
Different variables per branchAllowedUsually compares one variable only
Range checks (e.g. 1–10)Easy with relational operatorsBest for exact matches; use IF for ranges
Default/fallbackFinal ELSEOTHERWISE

Choose CASE for tidy discrete choices. Choose IF for ranges, complex combinations, or when different variables are tested.

Edge cases and default handling

Always include a branch to handle unexpected or invalid values. For example, when reading a menu choice, the user might enter lower-case or a blank value. Normalise input (e.g. convert to upper case) before the CASE, and use OTHERWISE to give helpful feedback.

// CAIE Pseudocode
DECLARE raw : STRING ← "b"
DECLARE choice : CHAR
choice ← TOUPPER(raw[0])             // normalise
CASE OF choice
  'A': OUTPUT "Add"
  'B': OUTPUT "Browse"
  OTHERWISE OUTPUT "Please choose A or B."
ENDCASE

Designing correct conditions

Correct selection depends on accurate conditions. Think about boundaries, coverage, and exclusivity:

  • Boundaries: decide where each band starts and ends. Use >= or > consistently to avoid gaps and overlaps.
  • Coverage: ensure all realistic inputs are handled by some branch (use ELSE/OTHERWISE to catch the rest).
  • Exclusivity: write conditions so exactly one branch matches, unless you intentionally want several actions to happen.

Using Boolean operators

Combine tests with AND, OR, and NOT. Parentheses clarify grouping and prevent mistakes:

// CAIE Pseudocode (library example)
DECLARE age : INTEGER ← 14
DECLARE hasCard : BOOLEAN ← TRUE
IF (age >= 12 AND hasCard = TRUE) OR age >= 18 THEN
  OUTPUT "May borrow teen books."
ELSE
  OUTPUT "Ask a librarian."
ENDIF

# Python
age = 14
hasCard = True
if (age >= 12 and hasCard) or age >= 18:
    print("May borrow teen books.")
else:
    print("Ask a librarian.")

Adding parentheses makes your intent unambiguous and helps examiners follow your logic.

Selection in real contexts

  • Web forms: IF required fields are blank, show an error; OTHERWISE submit the form.
  • Games: IF health is 0, show Game Over; ELSEIF score reaches a threshold, level up; OTHERWISE continue.
  • Smart devices: CASE mode is 'Eco', 'Normal', or 'Turbo' to adjust motor speed.

Deep Dive: Ordering and boundary bugs

Suppose you award travel discounts: child (< 12), teen (12–17), adult (18+). If you test age >= 12 first, the teen branch will capture 18-year-olds if you write ELSEIF age >= 12 and forget the upper limit. Place stricter conditions first and make ranges explicit, e.g. ELSEIF age >= 12 AND age <= 17. A quick test set at the boundaries (11, 12, 17, 18) helps prove correctness.

Key terminology

  • Selection: control structure choosing between alternative paths based on a condition.
  • Condition: an expression that evaluates to TRUE or FALSE.
  • Branch: a block of statements that runs when its condition is chosen.
  • CASE statement: a selection structure that picks one branch based on the value of a single expression, with an OTHERWISE default.

 Key Takeaways

  • Use one-way IF for optional actions, IF..ELSE for two exclusive outcomes, and IF..ELSEIF or CASE for multiple choices.
  • CASE is clearer for discrete options of the same variable; IF suits ranges and complex tests.
  • Boundaries, coverage, and exclusivity prevent selection bugs and ensure exactly one correct branch fires.
  • Order matters in IF ladders: put stricter or higher-priority tests first.
  • Always include a sensible ELSE/OTHERWISE to handle unexpected inputs.