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

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Topic 11.2 Constructs

Topic 11.2 – Constructs

Learning Objective

Write pseudocode that demonstrates:

  • An IF statement with an ELSE clause.
  • Nested IF statements.

1. Basic IF‑ELSE Structure

The general form of an IF‑ELSE statement in pseudocode is:

ComponentPseudocode
ConditionIF condition THEN
True block  … statements …
Else blockELSE
False block  … statements …
EndEND IF

Example – deciding whether a student passes:

  • IF \$mark \ge 40\$ THEN
  •    output “Pass”
  • ELSE
  •    output “Fail”
  • END IF

2. Nested IF Statements

When a decision depends on more than one condition, an IF can be placed inside another IF block. The syntax remains the same; the inner IF is simply indented for readability.

General pattern:

  • IF outer‑condition THEN
  •    IF inner‑condition THEN
  •       … statements …
  •    ELSE
  •       … statements …
  •    END IF
  • ELSE
  •    … statements …
  • END IF

Example – grading a test based on two criteria:

  • IF \$score \ge 70\$ THEN
  •    output “Distinction”
  • ELSE
  •    IF \$score \ge 50\$ THEN
  •       output “Merit”
  •    ELSE
  •       output “Pass”
  •    END IF
  • END IF

3. Common Pitfalls

  1. Forgetting the END IF – leads to ambiguous block termination.
  2. Using assignment (=) instead of comparison (= = or ≠) in the condition.
  3. Mixing indentation levels, which makes the logic hard to follow.
  4. Placing an ELSE without a preceding IF block.

4. Practice Exercise

Write pseudocode to determine the shipping cost for an online order. The rules are:

  • 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.

Include both an IF‑ELSE version and a nested IF version.

Suggested diagram: Flowchart showing the decision process for the shipping cost exercise.