← IGCSE Computer Science Paper 2

Nested selection: an IF inside an IF

An IF placed inside the THEN or ELSE branch of another IF. It is only checked when that outer branch runs, and each IF needs its own ENDIF, with the inner ENDIF written first.

Understanding Nested selection: an IF inside an IF in IGCSE Computer Science Paper 2

What it does: Each level of IF is indented further in, so the student can see which ENDIF closes which IF. Checking Age >= 13 and then HasTicket = TRUE in a nested IF gives the same result as joining the two conditions with AND. An inner IF is part of the outer THEN or ELSE branch. If that branch is not chosen, the inner condition is never tested. The inner IF is closed first because it sits inside the outer IF. Each IF needs its own ENDIF. Every IF needs its own ENDIF, so two IFs need two ENDIFs. One ENDIF cannot close both.

Common mistakes: The inner IF has the operator written the wrong way round. 'Greater than or equal to' is >=, not =>. It should be IF mark >= 80 THEN. The inner IF is missing THEN. Every IF, including a nested one, must end its condition with THEN. It should be IF age < 12 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
A nested IF is checked only when the outer branch it sits inside actually runs. Which statement is correct?
The inner IF is skipped if its outer branch does not runThe inner IF is always checked, whatever the outer IF does
Matches
IF Age >= 13 THEN IF HasTicket = TRUE THEN OUTPUT "Enter" ENDIF
↓
The inner IF has no ENDIF of its own. The only ENDIF is used up by one IF, so the other IF is left unclosed.
Applying
Meanings
Term
nested IF
Meaning
An IF placed inside the THEN or ELSE branch of another IF. It is only checked when that outer branch runs, and each IF needs its own ENDIF, with the inner ENDIF written first.
Practice
INPUT Mark
Testing
Gaps
hasTicket = TRUE THEN
Check Answer
Spots
2 OUTPUT "Pass"
3 IF mark => 80 THEN

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

The 'Seal' badge — Smooth Formatter, 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. A nested IF is checked only when the outer branch it sits inside actually runs. Which statement is correct?
A. The inner IF is always checked, whatever the outer IF does
B. The inner IF is skipped if its outer branch does not run ✓
An inner IF is part of the outer THEN or ELSE branch. If that branch is not chosen, the inner condition is never tested.
2. Which is the correct order for closing two nested IF statements?
A. ENDIF (outer) first, then ENDIF (inner)
B. ENDIF (inner) first, then ENDIF (outer) ✓
The inner IF is closed first because it sits inside the outer IF. Each IF needs its own ENDIF.
3. IF age >= 16 THEN IF hasId = TRUE THEN OUTPUT "Enter" ENDIF ENDIF A student has age 14. What happens?
A. Nothing is output, because the inner IF is never checked ✓
B. The inner IF is checked and then fails, so "Enter" is not output
The outer condition age >= 16 is false, so the THEN branch is skipped. The inner IF never runs, so hasId is never tested.
4. How many ENDIF statements does this code need? IF a > 0 THEN IF b > 0 THEN OUTPUT "Both"
A. Two: one for each IF ✓
B. One: the outer ENDIF covers both IFs
Every IF needs its own ENDIF, so two IFs need two ENDIFs. One ENDIF cannot close both.
5. Which nested IF is placed inside the ELSE branch of the outer IF?
A. IF score >= 50 THEN OUTPUT "Pass" ELSE IF score >= 40 THEN OUTPUT "Near" ENDIF ENDIF ✓
B. IF score >= 50 THEN IF score >= 40 THEN OUTPUT "Near" ENDIF ENDIF
The first example puts the inner IF after the outer ELSE, so it only runs when score >= 50 is false. In the second, the inner IF is inside THEN.

Match the Pairs

IF Age >= 13 THEN IF HasTicket = TRUE THEN OUTPUT "Enter" ENDIF ENDIF → The inner IF checks the ticket only when the outer condition Age >= 13 is true, and each IF has its own ENDIF.
IF Score >= 50 THEN OUTPUT "Pass" ELSE IF Score >= 40 THEN OUTPUT "Resit" ENDIF ENDIF → The inner IF is only checked when Score >= 50 is false, so it handles the scores that failed the first test.
Indentation shows the levels of nesting: the inner IF, its statements and its ENDIF are indented further than the outer IF. → IF X > 0 THEN IF X < 10 THEN OUTPUT "Small" ENDIF ENDIF
A nested IF can often be replaced by one IF with AND, when both conditions must be true and there is no ELSE. → IF Age >= 13 AND HasTicket = TRUE THEN OUTPUT "Enter" ENDIF
IF Age >= 13 THEN IF HasTicket = TRUE THEN OUTPUT "Enter" ENDIF → The inner IF has no ENDIF of its own. The only ENDIF is used up by one IF, so the other IF is left unclosed.
IF Score >= 50 THEN IF Score >= 80 THEN OUTPUT "Merit" ENDIF ENDIF → The ENDIFs are in the wrong order or indentation. The outer ENDIF is written before the inner ENDIF, so the nesting is muddled.

Key Term

nested IF
An IF placed inside the THEN or ELSE branch of another IF. It is only checked when that outer branch runs, and each IF needs its own ENDIF, with the inner ENDIF written first.

Fill in the Blanks

1. IF age >= 16 THEN IF hasTicket = TRUE THEN OUTPUT "Enter"
Hint: Type the keyword that starts a second selection inside the THEN branch of the outer one.
2. OUTPUT "Pass" ENDIF ENDIF
Hint: Type the keyword that closes the inner selection first, before the outer one is closed.
3. ELSE IF temp < 10 THEN OUTPUT "Cold"
Hint: Type the keyword that starts the alternative branch of the outer IF, where the inner IF sits.
4. IF age >= 16 AND hasId = TRUE THEN OUTPUT "Enter" ENDIF
Hint: Type the logical operator that replaces nesting by joining both conditions in one IF.
5. IF raining = TRUE THEN IF windy = TRUE THEN OUTPUT "Stay in"
Hint: Type the keyword that ends the condition of the indented inner IF.

Spot the Error

Question 1
A IF mark >= 50 THEN
B OUTPUT "Pass"
C IF mark => 80 THEN
D OUTPUT "Distinction"
E ENDIF
F ENDIF
Line C — The inner IF has the operator written the wrong way round. 'Greater than or equal to' is >=, not =>. It should be IF mark >= 80 THEN.
Question 2
A IF hasTicket = TRUE THEN
B OUTPUT "Welcome"
C IF age < 12
D OUTPUT "Child seat"
E ENDIF
F ENDIF
Line C — The inner IF is missing THEN. Every IF, including a nested one, must end its condition with THEN. It should be IF age < 12 THEN.
Question 3
A IF hasID = TRUE THEN
B IF hasID <- TRUE THEN
C OUTPUT "Can enter"
D ENDIF
E ENDIF
Line B — The inner condition uses <- (assignment) instead of a comparison. A condition must compare values with =. It should be IF hasID = TRUE THEN.
Question 4
A IF age >= 18 DO
B IF hasID = TRUE THEN
C OUTPUT "Entry allowed"
D ENDIF
E ENDIF
Line A — DO belongs to WHILE loops. An outer IF must use THEN to start its branch. It should be IF age >= 18 THEN.
Question 5
A IF age >= 13 AND <= 19 THEN
B OUTPUT "Teenager"
C ENDIF
Line A — Each side of AND must be a full condition. The second one is missing age. It should be IF age >= 13 AND age <= 19 THEN. This one IF does the work of two nested IFs.

Student with a class code? Join your class →