Cambridge IGCSE · Topic 8.1
IGCSE Computer Science 0478 / Topic 8 / IGCSE

Choose a path.

Programs make decisions: pass or fail, valid or invalid, which menu option was chosen. Selection lets a program test a condition and run different statements depending on the result, using IF statements, nested IFs and CASE statements.

The key idea

Sequence means statements run in order, one after another. Selection means the program chooses which statements to run by testing a condition that is either TRUE or FALSE. An IF statement runs the THEN branch when the condition is TRUE and the optional ELSE branch when it is FALSE. A nested IF is an IF inside another IF. A CASE statement compares one variable with several single values and runs only the first match; OTHERWISE catches every other value.

INPUT Mark Mark >= 40 OUTPUT "Pass" OUTPUT "Fail" ENDIF TRUEFALSE THEN ELSE
Fig. 1: an IF statement follows one of two paths, then both paths meet again at ENDIF
Section 01

IF ... THEN ... ELSE.

In the lab, choose the Nested IF program and step through it. Change the input mark and watch which branch the program follows. THEN and ELSE are indented two spaces; the statements inside them are indented further.

DECLARE Age : INTEGER
INPUT Age
IF Age >= 18
  THEN
    OUTPUT "You can vote"
  ELSE
    OUTPUT "Too young to vote"
ENDIF

The same idea in Python (for comparison only: the exam uses pseudocode)

age = int(input())
if age >= 18:
    print("You can vote")
else:
    print("Too young to vote")
Examiner trap

Check the boundary. With IF Age > 18 an 18-year-old would be told they are too young. Use >= when the boundary value itself should pass.

Section 02

Nested IF.

An IF statement can sit inside the THEN or ELSE branch of another IF. Each inner level is indented further. You will not be asked to write more than three levels of nesting.

IF Mark >= 70
  THEN
    OUTPUT "Distinction"
  ELSE
    IF Mark >= 40
      THEN
        OUTPUT "Pass"
      ELSE
        OUTPUT "Fail"
    ENDIF
ENDIF
Worked example Dry run
What does the program above output when Mark is 70, and when Mark is 39?
  1. Mark = 70: is 70 >= 70? TRUE, so output Distinction. The ELSE branch (and the inner IF) is skipped.
  2. Mark = 39: is 39 >= 70? FALSE, so go to ELSE.
  3. Inner IF: is 39 >= 40? FALSE, so the inner ELSE runs.
Answer: 70 gives Distinction; 39 gives Fail
Your turn
What is output when Mark is 40?
Examiner trap

Test the highest band first. If the program tested Mark >= 40 first, a mark of 85 would output Pass and never reach Distinction.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

In the lab's Nested IF program with the input 55, what is output?

Quick check+10 XP

When does the ELSE branch of an IF statement run?

Quick check+10 XP

In a CASE statement, when does the OTHERWISE clause run?

Quick check+10 XP

Which keyword closes an IF statement in 0478 pseudocode?

Section 03

CASE OF.

When one variable is compared with several single values, a CASE statement is shorter and easier to read than a chain of IFs. Each case is indented two spaces. The cases are tested in order and only the first match runs.

DECLARE Choice : INTEGER
INPUT Choice
CASE OF Choice
  1 : OUTPUT "Start game"
  2 : OUTPUT "Load game"
  3 : OUTPUT "Quit"
  OTHERWISE OUTPUT "Invalid choice"
ENDCASE

The same idea in Python (for comparison only: the exam uses pseudocode)

choice = int(input())
if choice == 1:
    print("Start game")
elif choice == 2:
    print("Load game")
elif choice == 3:
    print("Quit")
else:
    print("Invalid choice")
Worked example The lab's CASE and REPEAT program
Position starts at 50. The cases are 'W' (subtract 10), 'E' (add 10), 'A' (subtract 1) and 'D' (add 1), with OTHERWISE OUTPUT "Beep". The loop repeats until Move = 'Q'. The inputs are W, D, D, X, Q. What is output?
  1. W: Position ← 50 - 10 = 40.
  2. D: 41. D: 42.
  3. X matches no case, so OTHERWISE outputs Beep.
  4. Q also matches no case, so OTHERWISE outputs Beep again; then UNTIL Move = 'Q' is TRUE and the loop ends.
  5. The last line outputs the final position.
Answer: Beep, Beep, Final position 42
Examiner trap

OTHERWISE must be the last case, and it runs only if none of the cases above it match. Use an IF, not CASE, when a branch depends on a range such as Mark >= 40.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

IF Temp > 30 THEN output "Hot", ELSE (IF Temp > 15 THEN output "Warm" ELSE output "Cold"). What is output when Temp is 30?

Exam style+20 XP

Why would a programmer use a CASE statement instead of several nested IF statements?

Exam style+20 XP

For the menu program in this lesson, the user enters 4. What is output?

Exam style+20 XP

Which condition is TRUE only for a mark from 0 to 100 inclusive?

Skill unlocked

Selection, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
IF statements, nested IFs and CASE statements: dry-running them and writing them in 0478 pseudocode.