Use pseudocode to write: an 'IF' statement including the 'ELSE' clause and nested IF statements

Topic 11.2 – Constructs (Cambridge AS & A‑Level Computer Science 9618)

Learning objectives

  • Write correct pseudocode for the selection structures required by the syllabus:

    • Simple selection (IF‑ELSE)
    • Multiple selection (CASE / SWITCH)
    • Nested IF statements

  • Explain why a particular selection structure is the most appropriate for a given problem (AO2, AO3).


1. Simple selection – IF‑ELSE

1.1 Formal syntax

ComponentPseudocode
Start of testIF condition THEN
True block … statements …
Else clause (optional)ELSE
False block (optional) … statements …
End of constructEND IF

1.2 Simple example – Pass / Fail

IF mark ≥ 40 THEN

OUTPUT "Pass"

ELSE

OUTPUT "Fail"

END IF

1.3 When to use IF‑ELSE

  • Only two possible outcomes (true / false).
  • The decision depends on a logical expression, not a single variable value.
  • When the “else” part is required to handle the false case.

1.4 Common pitfalls

  1. Forgetting END IF – the compiler cannot determine where the block finishes.
  2. Using the assignment operator (=) instead of a comparison operator (= =, , <, >).
  3. Inconsistent indentation – makes nested structures hard to read.
  4. Placing ELSE without a preceding IF or writing two ELSE clauses.


2. Nested IF statements

2.1 General pattern

IF outer_condition THEN

IF inner_condition THEN

… statements …

ELSE

… statements …

END IF

ELSE

… statements …

END IF

2.2 Example – Grading with two criteria

IF score ≥ 70 THEN

OUTPUT "Distinction"

ELSE

IF score ≥ 50 THEN

OUTPUT "Merit"

ELSE

OUTPUT "Pass"

END IF

END IF

2.3 Tips for readable nesting

  • Indent each level by a consistent amount (e.g., 4 spaces or one tab).
  • Keep nesting to a maximum of three levels; deeper logic is often clearer as a CASE statement or a separate function.
  • Comment the purpose of each test, especially when the conditions are complex.
  • Use meaningful variable names (e.g., is_premium rather than m).


3. Multiple‑selection – CASE / SWITCH (brief overview)

3.1 When to prefer CASE

  • The decision is based on a single expression that can take several discrete values.
  • Often clearer and less error‑prone than a long chain of IF‑ELSE statements.

3.2 Formal syntax

ComponentPseudocode
StartCASE expression OF
Branch value1: … statements …
Branch value2: … statements …
Default OTHER: … statements …
EndEND CASE

3.3 Example – Grade classification

CASE mark OF

90 TO 100: OUTPUT "A+"

80 TO 89 : OUTPUT "A"

70 TO 79 : OUTPUT "B"

60 TO 69 : OUTPUT "C"

OTHER : OUTPUT "Fail"

END CASE


4. Linking selection structures to the assessment objectives

  • AO1 – Knowledge: Recognise the four families of control structures and their formal syntax.
  • AO2 – Analysis: Choose the most suitable structure for a problem description (e.g., IF‑ELSE for two outcomes, CASE for many discrete values, nesting when decisions depend on several independent conditions).
  • AO3 – Design & Development: Write clear, correctly indented pseudocode that follows the Cambridge conventions.


5. Practice exercise – Shipping‑cost calculator

Problem statement

  • If the order total is at least £100, shipping is free.
  • Otherwise, if the customer is a “Premium” member, shipping costs £5.
  • Otherwise, shipping costs £10.

5.1 Solution using a single IF‑ELSE chain

IF order_total ≥ 100 THEN

shipping_cost ← 0

ELSE

IF membership = "Premium" THEN

shipping_cost ← 5

ELSE

shipping_cost ← 10

END IF

END IF

OUTPUT "Shipping cost: £", shipping_cost

5.2 Same logic written as a nested IF (different layout)

IF order_total ≥ 100 THEN

shipping_cost ← 0

ELSE

IF membership = "Premium" THEN

shipping_cost ← 5

ELSE

shipping_cost ← 10

END IF

END IF

OUTPUT "Shipping cost: £", shipping_cost

5.3 Alternative using CASE (when the first test fails)

IF order_total ≥ 100 THEN

shipping_cost ← 0

ELSE

CASE membership OF

"Premium": shipping_cost ← 5

OTHER : shipping_cost ← 10

END CASE

END IF

OUTPUT "Shipping cost: £", shipping_cost

5.4 Checklist for the exam

  • Correct use of IF … THEN, ELSE and END IF.
  • Proper indentation to show nesting.
  • Use of comparison operators (, =) rather than assignment.
  • All variables are initialised before they are used.


6. Summary – Choosing the right selection structure

StructureBest forKey feature
IF‑ELSETwo possible outcomes, or a simple true/false test.Explicit true and false blocks; easy to nest.
CASE / SWITCHOne variable with many discrete values.One‑line branches; default (OTHER) branch handles unexpected values.
Nested IFMultiple independent conditions that must be evaluated in sequence.Hierarchical decision‑making; can become hard to read if over‑nested.