7.1 What is a Nested IF?
- A nested IF is an IF statement written inside the THEN or ELSE branch of another IF.
- It lets you test a second condition only after the first one has been resolved.
- Use it for grading systems, age brackets, or any decision with more than two outcomes.
- Each IF needs its own ENDIF — count them carefully.
Key idea: A nested IF says "if A is true, then ALSO check B". The inner test only happens once the outer test has decided which branch to enter.
A Real Example — Grade Classifier
DECLARE Mark : INTEGER
INPUT Mark
IF Mark >= 75 THEN
OUTPUT "Grade A"
ELSE
IF Mark >= 60 THEN
OUTPUT "Grade B"
ELSE
IF Mark >= 50 THEN
OUTPUT "Grade C"
ELSE
OUTPUT "Grade F"
ENDIF
ENDIF
ENDIFIf the user enters 65, the outer condition (Mark >= 75) is FALSE, so we enter the ELSE and check the next IF. 65 >= 60 is TRUE, so "Grade B" is output. The deepest IF (for C and F) is skipped entirely.
What is a Nested IF?
7.2 Nested IF Syntax
The general shape of a nested IF (with the inner IF inside the ELSE branch) is:
IF <condition 1> THEN
<statements if condition 1 is TRUE>
ELSE
IF <condition 2> THEN
<statements if condition 1 FALSE and condition 2 TRUE>
ELSE
<statements if both conditions are FALSE>
ENDIF
ENDIF- IF <condition> THEN — opens each IF block.
- ELSE — optional, separates the TRUE and FALSE branches of an IF.
- ENDIF — closes one IF. Write the innermost ENDIF first, the outermost last.
- Indent each nesting level by 2 spaces — for readability only, but essential for clarity.
Worked Example: Pass / Merit / Distinction
DECLARE Mark : INTEGER
INPUT Mark
IF Mark >= 75 THEN
OUTPUT "Distinction"
ELSE
IF Mark >= 50 THEN
OUTPUT "Merit"
ELSE
OUTPUT "Fail"
ENDIF
ENDIFENDIF count rule: Every IF needs its own ENDIF. If you have 3 nested IFs, you need 3 ENDIFs at the bottom — in the correct order (innermost first, outermost last).
Nested IF Syntax
7.3 How Nested IF Works (Tracing)
Tracing a nested IF means evaluating each condition top-to-bottom and writing down TRUE or FALSE next to it. As soon as one THEN (or ELSE) branch runs, the program jumps to after the outermost ENDIF — no other branch can run.
Worked Trace: Score = 75
DECLARE Score : INTEGER
Score <- 75
IF Score >= 90 THEN
OUTPUT "A"
ELSE
IF Score >= 70 THEN
OUTPUT "B"
ELSE
OUTPUT "C"
ENDIF
ENDIF- Score = 75. Is 75 >= 90? FALSE → enter the outer ELSE
- Is 75 >= 70? TRUE → run the inner THEN, OUTPUT "B"
- The inner ELSE ("C") is skipped; control jumps to after the outer ENDIF
Worked Trace: X = 5
DECLARE X : INTEGER
X <- 5
IF X > 10 THEN
OUTPUT "A"
ELSE
IF X > 3 THEN
OUTPUT "C"
ELSE
OUTPUT "D"
ENDIF
ENDIFStep-by-step: X is 5. Is 5 > 10? No — so we go into the outer ELSE. Now is 5 > 3? Yes — so we run the inner THEN and OUTPUT "C". The inner ELSE ("D") is never reached.
Exam tip: When tracing nested IFs in an exam, write TRUE or FALSE next to each condition as you go. This makes it almost impossible to lose track of which branch executes.
How Nested IF Works (Tracing)
7.4 Nesting in THEN vs ELSE
You can nest an IF inside the THEN branch (to refine a TRUE case) or inside the ELSE branch (to refine a FALSE case). Both are valid — choose the one that makes the logic clearest.
Nesting in ELSE — Grade Brackets
Grade brackets (A >= 75, B >= 60, C >= 50) are most naturally written by nesting each new test inside the ELSE of the previous one. Each ELSE rules out the previous (higher) bracket, so the next IF only needs to test the next lower threshold.
IF Mark >= 75 THEN
OUTPUT "A"
ELSE
IF Mark >= 60 THEN
OUTPUT "B"
ELSE
IF Mark >= 50 THEN
OUTPUT "C"
ELSE
OUTPUT "F"
ENDIF
ENDIF
ENDIFNesting in THEN — Refining a TRUE Case
If you only want to check a sub-condition when the outer IF is TRUE, nest inside THEN.
IF Age >= 18 THEN
IF HasLicence THEN
OUTPUT "Can drive"
ELSE
OUTPUT "Needs a licence first"
ENDIF
ELSE
OUTPUT "Too young to drive"
ENDIFRule of thumb: Nesting in ELSE is ideal for ordered ranges (grade brackets, tax bands). Nesting in THEN is ideal when a sub-test only makes sense once the outer condition is TRUE (age + licence).
Nesting in THEN vs ELSE
7.5 Nested IF vs CASE...OF
Both nested IF and CASE...OF can express multi-way decisions, but they shine in different situations.
| Feature | Nested IF | CASE...OF |
|---|---|---|
| Tests how many variables? | Many (one per level) | One only |
| Best for | Ranges and complex conditions | Exact value matching |
| Compound conditions (AND/OR)? | Yes — easy | No — labels are exact values |
| Readability with many branches | Gets deeply indented | Stays flat — one line per case |
| End keyword | ENDIF (one per IF) | ENDCASE (one per CASE) |
Same Logic, Two Styles
Using Nested IF (ranges)
IF Mark >= 75 THEN
OUTPUT "A"
ELSE
IF Mark >= 50 THEN
OUTPUT "B"
ELSE
OUTPUT "C"
ENDIF
ENDIFUsing CASE...OF (exact values)
CASE OF Grade
'A' : OUTPUT "Excellent"
'B' : OUTPUT "Good"
'C' : OUTPUT "Pass"
OTHERWISE OUTPUT "Fail"
ENDCASERule of thumb: Use nested IF for ranges and multi-variable logic. Use CASE...OF for one variable with several exact values. Any CASE can be rewritten as nested IF and vice versa.
Nested IF vs CASE...OF
7.6 Common Mistakes & Key Points
Here are the most common mistakes with nested IFs — and the key points to remember.
Watch out: The single most common mistake is a missing or mismatched ENDIF. For every IF you open, count one ENDIF — and make sure they close in the right order (innermost first).
Exam tip: If a question shows thresholds (>= 75, >= 60, >= 50), reach for nested IF. If it shows named distinct values (1, 2, 3, "A", "B"), reach for CASE...OF.