← IGCSE Computer Science Paper 2

Nested IF statements (7a)

An IF statement written inside the THEN or ELSE part of another IF. It only runs when the outer condition leads to it, and each IF needs its own ENDIF.

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

What it does: The second IF sits inside the ELSE part, so it only runs when Age >= 18 is false. One IF gives two outcomes. A nested IF in the ELSE splits it again to give three outcomes, and each level is indented two more spaces. Every IF needs its own ENDIF. The inner ENDIF closes the inner IF and the outer ENDIF closes the outer IF. The outer condition 40 >= 50 is False, so the program skips the THEN part and never reaches the inner IF. Add a second ENDIF at the end to close the outer IF. Two IF statements need two ENDIF lines.

Common mistakes: An IF statement must use THEN after its condition, not DO. The outer IF should read IF Mark >= 50 THEN. The ← symbol is for assignment. A condition compares values with =, so the nested IF should read IF HasPermission = TRUE 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 37–46) and word search (Modules 27–46).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which describes a nested IF?
An IF statement written inside the THEN or ELSE part of another IFTwo separate IF statements written one after the other
Matches
IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Merit" ENDIF
↓
The outer IF has no ENDIF, because only the inner IF was closed. Each IF needs its own ENDIF.
Applying
Meanings
Term
nested IF
Meaning
An IF statement written inside the THEN or ELSE part of another IF. It only runs when the outer condition leads to it, and each IF needs its own ENDIF.
Practice
IF Mark >= 70
Testing
Gaps
Score >= 80 THEN
Check Answer
Spots
1IF Mark >= 50 DO
2 IF Mark >= 80 THEN

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

The 'Barreleye Fish' badge — Clear Vision, 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 describes a nested IF?
A. Two separate IF statements written one after the other
B. An IF statement written inside the THEN or ELSE part of another IF ✓
A nested IF sits inside another IF's THEN or ELSE part. Two IFs one after the other are not nested.
2. When does the inner IF of a nested IF run?
A. Only when the outer condition leads to it ✓
B. Every time the program runs
The inner IF is only reached when the outer IF sends the program into the part that contains it.
3. A program has one IF with another IF inside its THEN part. How many ENDIFs does it need?
A. Two, one for each IF ✓
B. One, shared by both IFs
Every IF needs its own ENDIF. The inner ENDIF closes the inner IF and the outer ENDIF closes the outer IF.
4. Which shows correct indentation for a nested IF?
A. The inner IF starts at the same position as the outer IF
B. The inner IF is indented a further two spaces inside the outer IF ✓
Indentation continues by two spaces for each level, so the inner IF lines up with the code inside the outer IF.
5. Where should the inner ENDIF be written?
A. Before the outer ENDIF, lined up with the inner IF ✓
B. After the outer ENDIF, lined up with the outer IF
The inner IF must be closed first. Its ENDIF lines up with the inner IF, and the outer ENDIF comes after it.

Match the Pairs

IF Mark >= 50 THEN IF Mark >= 80 THEN → The second IF is nested inside the THEN part, so it is only checked when Mark is at least 50
ENDIF ENDIF → Each IF has its own ENDIF: the inner one closes first, then the outer one
A nested IF can be put in the ELSE part, so it is only reached when the outer condition is false → IF Age >= 18 THEN OUTPUT "Adult" ELSE IF Age >= 13 THEN
Choosing between three outcomes needs a nested IF, with the code indented two more spaces → IF Score >= 50 THEN OUTPUT "Pass" ELSE IF Score >= 40 THEN OUTPUT "Near" ELSE OUTPUT "Fail" ENDIF ENDIF
IF Mark >= 50 THEN IF Mark >= 80 THEN OUTPUT "Merit" ENDIF → The outer IF has no ENDIF, because only the inner IF was closed. Each IF needs its own ENDIF.
IF Mark >= 50 THEN OUTPUT "Pass" ELSE IF Mark >= 40 THEN OUTPUT "Near" ENDIF ENDIF → The nested IF is not indented by two more spaces, so it is hard to see that it sits inside the ELSE.

Key Term

nested IF
An IF statement written inside the THEN or ELSE part of another IF. It only runs when the outer condition leads to it, and each IF needs its own ENDIF.

Fill in the Blanks

1. IF Score >= 50 THEN IF Score >= 80 THEN OUTPUT "Distinction"
Hint: Type the keyword that starts the inner statement, written inside the THEN part of the outer one.
2. IF Age >= 13 THEN IF Age <= 19 THEN OUTPUT "Teenager"
Hint: Type the keyword that follows the inner condition and starts the code that runs when it is true.
3. OUTPUT "Distinction" ENDIF ENDIF
Hint: Type the keyword that closes the inner IF. Every IF needs its own closing keyword.
4. IF Mark >= 50 THEN OUTPUT "Pass" ELSE
Hint: Type the keyword that begins the alternative part, where another IF can be nested for a second check.
5. IF Temp > 0 THEN IF Temp < 25 THEN OUTPUT "Mild"
Hint: Type the relational operator meaning 'less than' so the inner check tests an upper limit.

Spot the Error

Question 1
A IF Mark >= 50 DO
B IF Mark >= 80 THEN
C OUTPUT "Distinction"
D ENDIF
E ENDIF
Line A — An IF statement must use THEN after its condition, not DO. The outer IF should read IF Mark >= 50 THEN.
Question 2
A IF Age >= 13 THEN
B IF HasPermission ← TRUE THEN
C OUTPUT "Welcome"
D ENDIF
E ENDIF
Line B — The ← symbol is for assignment. A condition compares values with =, so the nested IF should read IF HasPermission = TRUE THEN.
Question 3
A IF Age >= 18 THEN
B IF HasLicence = TRUE THEN
C OUTPUT "Can drive"
D ENDWHILE
E ENDIF
Line D — Every IF, including a nested one, must be closed with its own ENDIF. ENDWHILE belongs to a WHILE loop, so this line should be ENDIF.
Question 4
A INPUT Mark
B IF Mark >= 50 THEN
C IF Mark => 80 THEN
D OUTPUT "Merit"
E ENDIF
F ENDIF
Line C — The 'greater than or equal to' operator is >=, not =>. The nested IF should read IF Mark >= 80 THEN.
Question 5
A OUTPUT Age
B IF Age >= 13 THEN
C IF Age <= 19 THEN
D OUTPUT "Teenager"
E ENDIF
F ENDIF
Line A — The program must read the age with INPUT so the IF statements have a value to test. OUTPUT would only display Age, so line 1 should be INPUT Age.

Student with a class code? Join your class →