← IGCSE Computer Science Paper 2

Several alternatives with IF inside ELSE

A nested IF placed inside ELSE to choose between 3+ alternatives. Conditions are tested in order, only one branch runs, and every IF needs its own ENDIF.

Understanding Several alternatives with IF inside ELSE in IGCSE Computer Science Paper 2

What it does: The highest boundary (80) is tested first, then the next one down (50). Testing 50 first would give a mark of 85 a B. The outer IF and the nested IF each have a matching ENDIF. The inner ENDIF comes first, then the outer one. An IF can only have one ELSE. To get a third alternative, put a second IF inside the ELSE branch. Each IF needs its own ENDIF, so two IFs need two ENDIFs. The program does not look for the best match. It runs the first branch that is true, so the order of the conditions matters.

Common mistakes: The inner IF must be closed with its own ENDIF. ENDCASE belongs to a CASE statement. Line 4 should be ' ENDIF'. Conditions are tested in order, so the highest boundary must come first. Testing 60 first gives A to every mark from 60 up, and the B branch can never run. Line 1 should test Pts >= 80.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 369–378) and word search (Modules 358–378).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which structure chooses between three alternatives in Cambridge pseudocode?
IF ... THEN ... ELSE ... ELSE ... ENDIFIF ... THEN ... ELSE IF ... THEN ... ELSE ... ENDIF ENDIF
Matches
IF Mark >= 50 THEN Grade <- "C" ELSE IF Mark >= 80 THEN Grade <- "A" ENDIF ENDIF
↓
The conditions are in the wrong order. Any mark of 50 or more is caught by the first IF, so the test for 80 can never run and A is never given.
Applying
Meanings
Term
ELSE IF
Meaning
A nested IF placed inside ELSE to choose between 3+ alternatives. Conditions are tested in order, only one branch runs, and every IF needs its own ENDIF.
Practice
INPUT Mark
Testing
Gaps
Mark >= 50 THEN
Check Answer
Spots
3 IF Pts >= 60 THEN OUTPUT "B"
4 ENDCASE

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Mantis Shrimp' badge — Speed Striker, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which structure chooses between three alternatives in Cambridge pseudocode?
A. IF ... THEN ... ELSE IF ... THEN ... ELSE ... ENDIF ENDIF ✓
B. IF ... THEN ... ELSE ... ELSE ... ENDIF
An IF can only have one ELSE. To get a third alternative, put a second IF inside the ELSE branch.
2. A grade program has IF, then ELSE IF, then ELSE. How many ENDIF statements does it need?
A. 2, one for every IF ✓
B. 1, because there is only one ELSE chain
Each IF needs its own ENDIF, so two IFs need two ENDIFs.
3. Mark is 85. The code checks IF Mark >= 70 THEN, then ELSE IF Mark >= 50 THEN. How many branches run?
A. Both, because 85 is also at least 50
B. Only one: the first branch whose condition is true ✓
Once a condition is true, that branch runs and the rest of the ELSE part is skipped. Only one branch ever runs.
4. Which order is correct for checking grade boundaries?
A. Highest boundary first: Mark >= 70, then Mark >= 50, then Mark >= 30 ✓
B. Lowest boundary first: Mark >= 30, then Mark >= 50, then Mark >= 70
Conditions are tested in order. If you test Mark >= 30 first, a mark of 85 matches straight away and gets the lowest grade.
5. Mark is 85. The code tests IF Mark >= 30 THEN Grade <- "C" first. What happens?
A. Grade is set to "A", because the program finds the best match
B. Grade is set to "C", which is wrong, because the first true condition wins ✓
The program does not look for the best match. It runs the first branch that is true, so the order of the conditions matters.

Match the Pairs

ELSE IF Mark >= 60 THEN OUTPUT "Merit" ENDIF ENDIF → A second IF placed inside the ELSE branch, so it is only tested when the first condition was false.
IF Mark >= 70 THEN OUTPUT "Distinction" ELSE IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF ENDIF → Three alternatives where only one message is output, and it needs two ENDIFs because there are two IFs.
Grade boundaries must be ordered from the highest boundary down, so a mark of 85 is tested against 80 before 50. → IF Mark >= 80 THEN Grade <- "A" ELSE IF Mark >= 50 THEN Grade <- "B" ELSE Grade <- "C" ENDIF ENDIF
Every IF needs its own ENDIF, so a structure with two IFs closes with two ENDIF lines. → IF Score >= 40 THEN OUTPUT "Pass" ELSE IF Score >= 20 THEN OUTPUT "Resit" ELSE OUTPUT "Fail" ENDIF ENDIF
IF Mark >= 50 THEN Grade <- "C" ELSE IF Mark >= 80 THEN Grade <- "A" ENDIF ENDIF → The conditions are in the wrong order. Any mark of 50 or more is caught by the first IF, so the test for 80 can never run and A is never given.
IF Mark >= 70 THEN OUTPUT "Distinction" ELSE IF Mark >= 50 THEN OUTPUT "Pass" ELSE OUTPUT "Fail" ENDIF → The nested IF has no ENDIF of its own. Two IFs need two ENDIFs, but only one is written.

Key Term

ELSE IF
A nested IF placed inside ELSE to choose between 3+ alternatives. Conditions are tested in order, only one branch runs, and every IF needs its own ENDIF.

Fill in the Blanks

1. IF Mark >= 70 THEN OUTPUT "A" ELSE IF Mark >= 50 THEN
Hint: Type the keyword that starts a second selection inside the ELSE branch.
2. OUTPUT "B" ENDIF ENDIF
Hint: Type the keyword that closes the inner selection. Every IF needs its own closing keyword.
3. IF Mark >= 70 THEN OUTPUT "A" ELSE
Hint: Type the relational operator meaning 'greater than or equal to' for the highest boundary.
4. ELSE IF Mark >= 50 THEN OUTPUT "B"
Hint: Type the keyword that follows the condition and starts the statements to run.
5. IF Mark >= 70 THEN OUTPUT "A" ELSE
Hint: Type the keyword that begins the branch used when the first condition is false.

Spot the Error

Question 1
A IF Pts >= 80 THEN OUTPUT "A"
B ELSE
C IF Pts >= 60 THEN OUTPUT "B"
D ENDCASE
E ENDIF
Line D — The inner IF must be closed with its own ENDIF. ENDCASE belongs to a CASE statement. Line 4 should be ' ENDIF'.
Question 2
A IF Pts >= 60 THEN OUTPUT "A"
B ELSE
C IF Pts >= 60 THEN OUTPUT "B"
D ELSE OUTPUT "C"
E ENDIF
F ENDIF
Line A — Conditions are tested in order, so the highest boundary must come first. Testing 60 first gives A to every mark from 60 up, and the B branch can never run. Line 1 should test Pts >= 80.
Question 3
A IF Pct >= 85 THEN OUTPUT "A"
B OTHERWISE
C IF Pct >= 60 THEN OUTPUT "B"
D ELSE OUTPUT "C"
E ENDIF
F ENDIF
Line B — OTHERWISE is only used inside a CASE statement. To choose between more alternatives, the next IF goes inside an ELSE branch. Line 2 should be 'ELSE'.
Question 4
A IF Pts >= 75 THEN OUTPUT "A"
B ELSE
C IF Pts >= 50 OUTPUT "B"
D ELSE OUTPUT "C"
E ENDIF
F ENDIF
Line C — Every IF needs the keyword THEN after its condition, including an IF nested inside ELSE. Line 3 should be ' IF Pts >= 50 THEN OUTPUT "B"'.
Question 5
A IF Pct => 70 THEN OUTPUT "A"
B ELSE
C IF Pct >= 40 THEN OUTPUT "B"
D ELSE OUTPUT "C"
E ENDIF
F ENDIF
Line A — 'greater than or equal to' is written >= in Cambridge pseudocode. => is not a valid operator. Line 1 should be 'IF Pct >= 70 THEN OUTPUT "A"'.

Student with a class code? Join your class →