← IGCSE Computer Science Paper 2

Nested selection with CASE and IF

A CASE clause can hold an IF, and an IF branch can hold a CASE. Close the inner structure first with its own ENDIF or ENDCASE, then close the outer one.

Understanding Nested selection with CASE and IF in IGCSE Computer Science Paper 2

What it does: The IF opens after the CASE, so ENDIF comes before ENDCASE. The last structure opened is the first one closed. Any clause of a CASE can hold other statements, including a complete IF...ENDIF. The IF is inside the clause for 6, so it only runs when that clause is chosen. Every IF needs its own ENDIF. ENDCASE cannot close the inner IF, so the IF is left unfinished.

Common mistakes: A CASE statement must start with CASE OF followed by the variable. The word OF is missing. It should be CASE OF Choice. The outer IF line needs the keyword THEN after its condition. It should be IF Age >= 16 THEN.

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 389–398) and word search (Modules 379–398).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which shows an IF correctly nested inside a CASE clause?
CASE OF Grade 'A' : IF Score > 90 THEN OUTPUT "Top" ENDIF ENDCASECASE OF Grade 'A' : IF Score > 90 THEN OUTPUT "Top" ENDCASE
Matches
CASE OF Choice 1: IF Age >= 16 THEN OUTPUT "OK" ENDCASE
↓
The IF inside the CASE clause has no ENDIF. Add ENDIF after the OUTPUT line, before ENDCASE.
Applying
Meanings
Term
CASE OF ... IF ... ENDIF ... ENDCASE
Meaning
A CASE clause can hold an IF, and an IF branch can hold a CASE. Close the inner structure first with its own ENDIF or ENDCASE, then close the outer one.
Practice
CASE OF Choice
Testing
Gaps
1 : Score > 50 THEN OUTPUT "Pass" ENDIF
Check Answer
Spots
1CASE Choice
2 1 : IF Score > 50 THEN

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

The 'Parrotfish' badge — Function Builder, 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 shows an IF correctly nested inside a CASE clause?
A. CASE OF Grade 'A' : IF Score > 90 THEN OUTPUT "Top" ENDIF ENDCASE ✓
B. CASE OF Grade 'A' : IF Score > 90 THEN OUTPUT "Top" ENDCASE
The IF inside the clause needs its own ENDIF before the ENDCASE closes the CASE.
2. An IF branch contains a CASE statement. Which closing order is correct?
A. ENDCASE then ENDIF ✓
B. ENDIF then ENDCASE
Close the inner structure first. The CASE is inside the IF, so ENDCASE comes first, then ENDIF.
3. A CASE clause contains an IF. Which closing order is correct?
A. ENDCASE then ENDIF
B. ENDIF then ENDCASE ✓
The IF is the inner structure, so it is closed first with ENDIF. Then ENDCASE closes the outer CASE.
4. True or false: a CASE clause can run an IF statement.
A. False
B. True ✓
Any clause of a CASE can hold other statements, including a complete IF...ENDIF.
5. Which statement about nested selection is correct?
A. Each IF needs its own ENDIF and each CASE needs its own ENDCASE ✓
B. One ENDIF can close both an inner IF and the outer CASE
Every structure is closed by its own keyword. ENDIF cannot close a CASE and ENDCASE cannot close an IF.

Match the Pairs

CASE OF Choice 1: IF Age >= 16 THEN OUTPUT "OK" ENDIF ENDCASE → An IF statement placed inside a CASE clause. The IF is closed with ENDIF first, then the CASE is closed with ENDCASE.
IF Score > 0 THEN CASE OF Grade 'A': OUTPUT "Top" ENDCASE ENDIF → A CASE statement placed inside an IF branch. The CASE is closed with ENDCASE first, then the IF is closed with ENDIF.
The inner structure must be closed first, so the closing keywords appear in the reverse order to the opening ones: ENDIF then ENDCASE. → CASE OF Day 1: IF Wet = TRUE THEN OUTPUT "Bring coat" ENDIF ENDCASE
An IF branch can contain a CASE, and that CASE needs its own ENDCASE before the IF's ENDIF. → IF Member = TRUE THEN CASE OF Level 1: OUTPUT "Bronze" 2: OUTPUT "Silver" ENDCASE ENDIF
CASE OF Choice 1: IF Age >= 16 THEN OUTPUT "OK" ENDCASE → The IF inside the CASE clause has no ENDIF. Add ENDIF after the OUTPUT line, before ENDCASE.
IF Score > 0 THEN CASE OF Grade 'A': OUTPUT "Top" ENDIF ENDCASE → The closing keywords are in the wrong order. The inner CASE must end with ENDCASE before the outer IF ends with ENDIF.

Key Term

CASE OF ... IF ... ENDIF ... ENDCASE
A CASE clause can hold an IF, and an IF branch can hold a CASE. Close the inner structure first with its own ENDIF or ENDCASE, then close the outer one.

Fill in the Blanks

1. CASE OF Option 1 : IF Score > 50 THEN OUTPUT "Pass" ENDIF ENDCASE
Hint: Type the keyword that starts a selection inside this CASE clause.
2. CASE OF Option 2 : IF Lives = 0 THEN OUTPUT "Over" ENDIF ENDCASE
Hint: Type the keyword that closes the inner selection before the CASE ends.
3. CASE OF Option 1 : IF Score > 50 THEN OUTPUT "Pass" ENDIF ENDCASE
Hint: Type the keyword that closes the outer structure after the inner IF is closed.
4. IF Member = TRUE THEN CASE OF Level 1 : OUTPUT "Gold" ENDCASE ENDIF
Hint: Type the keyword that begins a multi-way choice inside the IF branch.
5. IF Age > 12 THEN CASE OF Level 1 : OUTPUT "Easy" ENDCASE ENDIF
Hint: Type the keyword that closes the outer IF, which must be closed last.

Spot the Error

Question 1
A CASE Choice
B 1 : IF Score > 50 THEN
C OUTPUT "Pass"
D ENDIF
E ENDCASE
Line A — A CASE statement must start with CASE OF followed by the variable. The word OF is missing. It should be CASE OF Choice.
Question 2
A IF Age >= 16
B CASE OF Role
C "S" : OUTPUT "Student"
D OTHERWISE OUTPUT "Other"
E ENDCASE
F ENDIF
Line A — The outer IF line needs the keyword THEN after its condition. It should be IF Age >= 16 THEN.
Question 3
A CASE OF Level
B 1 IF Lives = 0 THEN
C OUTPUT "Game over"
D ENDIF
E ENDCASE
Line B — Each CASE clause needs a colon between its value and the statement it runs. The line should be 1 : IF Lives = 0 THEN.
Question 4
A IF Open = TRUE THEN
B CASE OF Day
C "SAT" : OUTPUT Half day
D OTHERWISE OUTPUT "Full"
E ENDCASE
F ENDIF
Line C — Text to be output must be inside quote marks. Without them Half day looks like variable names. It should be OUTPUT "Half day".
Question 5
A CASE OF Grade
B "A" : IF Mark > 90 THEN
C OUTPUT "Top"
D ENDCASE
E ENDCASE
Line D — The inner IF must be closed with its own ENDIF before the CASE is closed. Using ENDCASE here is wrong. It should be ENDIF.

Student with a class code? Join your class →