Some decisions depend on an earlier decision. A website asks “are you logged in?” and only if the answer is yes does it ask “is your account an administrator?”. A nested conditional puts one conditional statement inside the block of another. The inner conditional is only reached when the outer condition sends the flow of control into that block. Nesting is how a program asks a second question after learning the answer to a first one.
Notation. The exam uses its own pseudocode, not a real programming language. The code in this pack is Marlbridge’s rendering of that reference notation, not a copy of the official sheet: ← is assignment, = tests equality, lists start at index 1, DISPLAY shows a value, and braces mark blocks. Use the Exam Reference Sheet from your teacher for the official wording.
What nesting means
A conditional statement can appear anywhere a statement can appear, including inside the block of another conditional. That gives the general shape below.
IF(outerCondition)
{
IF(innerCondition)
{
<result 1>
}
ELSE
{
<result 2>
}
}
ELSE
{
<result 3>
}
The rules are the same ones you already know, applied twice.
- Evaluate the outer condition first.
- If it is true, the first block runs, and inside that block the inner conditional is evaluated in the usual way.
- If it is false, the outer ELSE block (if there is one) runs, and the inner conditional is never looked at.
- Braces show which IF each ELSE belongs to: an ELSE pairs with the IF whose block closes just before it.
In the shape above there are three possible outcomes from two conditions, and you must test each one.
Nesting as a chain of ranges
A very common use is to put the next IF inside an ELSE. Each test is only reached when all the earlier tests failed, so the conditions can be simple:
IF(score ≥ 80)
{
grade ← "A"
}
ELSE
{
IF(score ≥ 60)
{
grade ← "B"
}
ELSE
{
grade ← "C"
}
}
Because the second test sits inside the ELSE of the first, it is only reached when score < 80, so there is no need to write score ≥ 60 AND score < 80. This also makes order matter: if the tests were reversed, a score of 90 would be caught by the ≥ 60 test first and receive a B.
Worked example 1: tracing a grade chain
Trace the grade chain above for scores 85, 70, 60 and 59.
| score | score ≥ 80 |
score ≥ 60 (only asked if the first was false) |
grade |
|---|---|---|---|
| 85 | true | not asked | A |
| 70 | false | true | B |
| 60 | false | true (boundary) | B |
| 59 | false | false | C |
Walk through 70. The outer test 70 ≥ 80 is false, so control enters the outer ELSE. There the inner conditional asks 70 ≥ 60, which is true, so grade becomes "B". The ELSE of the inner conditional is skipped.
Check. The boundary score 60 gives B because ≥ includes 60, and 59 is the highest score that gives C. Each of the three outcomes appears at least once in the table.
Order matters. If the code asked score ≥ 60 first, then 85 would receive B. To see why, trace it: the first test is true, so the block runs and the A test is never reached.
Worked example 2: the leap year
The rule for a leap year in the calendar is a famous nested decision: a year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years, unless they are also divisible by 400. Write it as a nested conditional and then simplify.
Step 1. Follow the questions in order.
- Divisible by 4? If no, not a leap year.
- If yes: divisible by 100? If no, it is a leap year.
- If yes: divisible by 400? If yes, leap; if no, not leap.
IF(year MOD 4 = 0)
{
IF(year MOD 100 = 0)
{
IF(year MOD 400 = 0)
{
leap ← true
}
ELSE
{
leap ← false
}
}
ELSE
{
leap ← true
}
}
ELSE
{
leap ← false
}
DISPLAY(leap)
Step 2. Test one value for each path.
| year | MOD 4 = 0 | MOD 100 = 0 | MOD 400 = 0 | leap |
|---|---|---|---|---|
| 2023 | false | not asked | not asked | false |
| 2024 | true | false | not asked | true |
| 1900 | true | true | false | false |
| 2000 | true | true | true | true |
Step 3. Flatten it. The same behaviour can be written as a single Boolean expression:
(year MOD 4 = 0 AND year MOD 100 ≠ 0) OR year MOD 400 = 0
Both forms agree on every year from 1 to 3,000 (we checked by running both in a program). The nested version reads like the questions a person would ask; the Boolean version is shorter. Neither is “more correct”, but being able to move between them is exactly the skill Topic 3.9 builds on.
Check. For 1900 the Boolean version gives (true AND false) OR false, which is false OR false, or false ✓. For 2000 it gives (true AND false) OR true, which is true ✓.
When can nesting be flattened?
IF(a) { IF(b) { X } } with no ELSE anywhere does the same thing as IF(a AND b) { X }: X runs only when both are true. But as soon as the inner conditional has its own ELSE (result 2 in Figure 1) or there is an outer ELSE, the nesting separates different outcomes and cannot be replaced by a single condition without extra tests. Use the simplest structure that still produces every required outcome.
A reliable way to trace a nested conditional
Nested code is easy to misread because the eye jumps between blocks. Use the same routine every time.
- Mark the braces. Draw a line from each opening brace to its matching closing brace. This shows exactly which statements belong to which block, and which IF each ELSE belongs to.
- Start at the outside. Evaluate the outermost condition and write
trueorfalsenext to it. - Cross out the block you are not entering. If the outer condition is true, cross out its ELSE block; if false, cross out its first block. Everything crossed out, including any inner conditionals inside it, is ignored.
- Repeat inside the block you entered. Evaluate the inner condition, again crossing out the unused block.
- Write down what ran. Only then read off the variable values or the displayed text.
This method also helps when you design nested code: list the outcomes you need (such as Result 1, Result 2, Result 3 in Figure 1), then ask which question separates them first. That first question becomes the outer condition.
Common misconceptions
- “The inner IF is always evaluated.” It is only evaluated when the outer condition sends control into its block.
- “An ELSE belongs to the nearest IF written above it.” It belongs to the IF whose block closes immediately before it; check the braces, not the layout.
- “The order of tests in a chain does not matter.” If the tests overlap (≥ 60 and ≥ 80), the order decides which block is chosen.
- “Two conditions need two test cases.” A nested conditional with two conditions has up to three paths, so you need a test for each.
- “Nesting is always better than AND.” For two conditions and a single outcome,
a AND bis shorter and just as clear. - “The inner ELSE runs when the outer condition is false.” The inner ELSE runs only when the outer condition is true and the inner condition is false.
Where this leads
Next comes iteration (Topic 3.8), where a condition decides whether to repeat a block. Equivalent conditionals and Boolean expressions are the main subject of Topic 3.9, where you will compare algorithms that look different but behave the same.
Next steps: try the practice questions, skim the revision notes the night before, and tick off the topic checklist. The next topic is Topic 3.8, Iteration.
Quick questions
What is a nested conditional?
A conditional statement placed inside a block of another conditional statement, so a second question is asked only after the first has been answered.
Does the inner IF always run?
No. It is evaluated only when the outer condition is true (or when it sits inside the outer ELSE and the outer condition is false).
Why does the order of tests matter in a chain?
Each test is only reached if earlier tests failed. If the conditions overlap, the first test that is true decides the outcome.
Can I replace a nested IF with AND?
Yes, when there is only one outcome and no ELSE blocks: IF(a) { IF(b) { X } } is the same as IF(a AND b) { X }.