← IGCSE Computer Science Paper 2

Nested IF statements (7c)

An IF statement placed inside the THEN or ELSE part of another IF, with its own ENDIF. The inner condition is only checked when the outer one sends the program into that part.

Understanding Nested IF statements (7c) in IGCSE Computer Science Paper 2

What it does: There are two IFs and two ENDIFs. The inner ENDIF closes the inner IF first, then the outer ENDIF closes the outer IF. The second IF is inside the ELSE part, so Temp > 15 is only checked when Temp > 30 is FALSE. Nested means one IF is inside another. Two IFs in a row are just a sequence of selections. Paper 2 uses nested IFs with at most three levels.

Common mistakes: The inner IF must be closed with its own ENDIF. ENDWHILE belongs to a WHILE loop. Line 4 should be ENDIF. The inner IF needs a comparison, not an assignment. ← stores a value, so it should be IF Temp > 40 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 156–165) and word search (Modules 146–165).

Take this worksheet further — free with Cloodit

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

Learning
Duo
In a nested IF, when is the inner condition checked?
Every time the program runs, before the outer conditionOnly when the outer condition sends the program into the part that holds the inner IF
Matches
IF X > 0 THEN IF X < 10 THEN OUTPUT "Small" ENDIF
↓
The inner IF has no ENDIF of its own. Two IFs need two ENDIFs, so the second ENDIF is missing.
Applying
Meanings
Term
nested IF
Meaning
An IF statement placed inside the THEN or ELSE part of another IF, with its own ENDIF. The inner condition is only checked when the outer one sends the program into that part.
Practice
IF Mark >= 50 THEN
Testing
Gaps
Age < 20 THEN
Check Answer
Spots
3 OUTPUT "Distinction"
4 ENDWHILE

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

The 'Gulper Eel' badge — Deep Streak, 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. In a nested IF, when is the inner condition checked?
A. Only when the outer condition sends the program into the part that holds the inner IF ✓
B. Every time the program runs, before the outer condition
The inner IF sits inside the THEN or ELSE part of the outer IF, so it is only reached when the program enters that part.
2. Which statement describes a nested IF?
A. Two separate IF statements written one after the other
B. An IF placed inside the THEN or ELSE part of another IF ✓
Nested means one IF is inside another. Two IFs in a row are just a sequence of selections.
3. How many ENDIF lines does this code need? IF Age >= 13 THEN IF Age <= 19 THEN OUTPUT "Teenager" ENDIF ENDIF
A. Two: one for each IF ✓
B. One: ENDIF only closes the outer IF
Every IF needs its own ENDIF, so two IFs need two ENDIFs. The inner ENDIF is written first.
4. Which ENDIF closes the inner IF in a nested IF?
A. The first ENDIF, written nearest to the inner IF ✓
B. The last ENDIF at the bottom of the code
Match IFs and ENDIFs like brackets. The inner IF closes first and the outer IF closes last.
5. A program checks whether a number is positive. If it is, a second IF checks whether it is even. Where does the second IF go?
A. After the ENDIF of the first IF
B. Inside the THEN part of the first IF ✓
The second check should only happen when the first one is TRUE, so it must be inside THEN.

Match the Pairs

IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Distinction" ENDIF ENDIF → A nested IF: the inner test Mark >= 80 is only checked when the outer test Mark >= 50 is TRUE.
IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN OUTPUT "Teen" ENDIF ENDIF → An IF inside the ELSE part: the second test is only checked when Age >= 18 is FALSE.
Every IF, including an inner IF, must be closed by its own ENDIF, and the inner ENDIF comes first. → IF Score > 0 THEN IF Score < 100 THEN OUTPUT "Valid" ENDIF ENDIF
A nested IF can be placed in the ELSE part so that a second condition is only tested if the first is FALSE. → IF Temp > 30 THEN OUTPUT "Hot" ELSE IF Temp > 15 THEN OUTPUT "Warm" ENDIF ENDIF
IF X > 0 THEN IF X < 10 THEN OUTPUT "Small" ENDIF → The inner IF has no ENDIF of its own. Two IFs need two ENDIFs, so the second ENDIF is missing.
IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN OUTPUT "Teen" ENDIF → The outer IF is never closed. The inner ENDIF only closes the inner IF, so a final ENDIF is missing.

Key Term

nested IF
An IF statement placed inside the THEN or ELSE part of another IF, with its own ENDIF. The inner condition is only checked when the outer one sends the program into that part.

Fill in the Blanks

1. IF Age >= 13 THEN IF Age < 20 THEN OUTPUT "Teen"
Hint: Type the keyword that starts a second selection inside the THEN part of the first.
2. OUTPUT "Teen" ENDIF ENDIF
Hint: Type the keyword that closes the inner (indented) selection before the outer one is closed.
3. IF Mark >= 80 THEN OUTPUT "Distinction" ELSE IF Mark >= 50 THEN OUTPUT "Pass"
Hint: Type the keyword that opens the part the program uses when the outer condition is FALSE, where the inner IF sits.
4. IF Age >= 18 THEN IF HasTicket = TRUE THEN OUTPUT "Enter"
Hint: Type the keyword that ends the inner condition and starts the code run only if it is true.
5. ENDIF ENDIF ENDIF
Hint: Three IFs are nested, so type the keyword that closes the outermost one, which is the last to be closed.

Spot the Error

Question 1
A IF Score >= 50 THEN
B IF Score >= 80 THEN
C OUTPUT "Distinction"
D ENDWHILE
E ENDIF
Line D — The inner IF must be closed with its own ENDIF. ENDWHILE belongs to a WHILE loop. Line 4 should be ENDIF.
Question 2
A IF Temp > 30 THEN
B IF Temp ← 40 THEN
C OUTPUT "Extreme"
D ENDIF
E ENDIF
Line B — The inner IF needs a comparison, not an assignment. ← stores a value, so it should be IF Temp > 40 THEN.
Question 3
A IF Age >= 18 THEN
B IF HasTicket = TRUE
C OUTPUT "Enter"
D ENDIF
E ENDIF
Line B — The inner IF is missing THEN. Every IF, nested or not, needs THEN after its condition: IF HasTicket = TRUE THEN.
Question 4
A INPUT Mark
B IF Mark >= 50 THEN
C IF Mark >= 80 THEN
D OUTPUT Distinction
E ENDIF
F ENDIF
Line D — The text to display must be in double quotes. Without them Distinction looks like a variable. It should be OUTPUT "Distinction".
Question 5
A INPUT Age
B IF Age >= 13 THEN
C IF Age >= 19 THEN
D OUTPUT "Teenager"
E ENDIF
F ENDIF
Line C — The inner condition uses the wrong relational operator. Teenagers are 19 or younger, so it should be IF Age <= 19 THEN.

Student with a class code? Join your class →