Article
Decisions inside decisions
A school gives grades: an A for 90 or more, otherwise a B for 80 or more, otherwise a C. One yes-or-no question cannot pick between three grades. The program needs to ask a question, and then, depending on the answer, ask another.
A decision inside a decision
A nested conditional is a conditional placed inside the block of another conditional. The inner one is only reached if the computer gets to that part of the program.
age ← 15
hasTicket ← true
IF(age ≥ 13)
{
IF(hasTicket)
{
DISPLAY("Enter")
}
ELSE
{
DISPLAY("Buy a ticket")
}
}
ELSE
{
DISPLAY("Too young")
}The same program as a tree of decisions:
age ≥ 13 ?
├─ true → hasTicket ?
│ ├─ true → Enter
│ └─ false → Buy a ticket
└─ false → Too youngFor age 15, the outer condition is true, so the computer goes into the first block. Only now does it test hasTicket. That is true, so the output is Enter. The inner question hasTicket is never asked when the age is under 13.
Change age to 10 and run it. The inner IF is skipped completely, and the output is Too young.
Check your understanding
What is displayed when this program runs?
n ← 12
IF(n > 10)
{
IF(n > 20)
{
DISPLAY("huge")
}
ELSE
{
DISPLAY("big")
}
}
ELSE
{
DISPLAY("small")
}A ladder of decisions
The most common nesting puts the next question inside an ELSE. This is how the grades work:
score ← 85
IF(score ≥ 90)
{
DISPLAY("A")
}
ELSE
{
IF(score ≥ 80)
{
DISPLAY("B")
}
ELSE
{
DISPLAY("C")
}
}| Step | Test | Result |
|---|---|---|
| 1 | 85 ≥ 90 | false, so go to the ELSE block |
| 2 | 85 ≥ 80 | true, so run the inner IF block |
| 3 | Display | B |
The second test is only made because the first one was false. So when the computer reaches score ≥ 80, it already knows the score is below 90.
Python has a shortcut for this ladder, elif. It does the same job as an ELSE with an IF inside. The AP exam does not use it, so on the exam you will always see the full nested form.
Check your understanding
What is displayed when this program runs?
score ← 65
IF(score ≥ 70)
{
DISPLAY("Pass")
}
ELSE
{
IF(score ≥ 50)
{
DISPLAY("Almost")
}
ELSE
{
DISPLAY("Fail")
}
}Order matters
Suppose the tests are in the wrong order:
score ← 95
IF(score ≥ 70)
{
DISPLAY("C")
}
ELSE
{
IF(score ≥ 90)
{
DISPLAY("A")
}
}95 is at least 70, so the first block runs and the program displays C. The test for 90 is never reached. Put the strictest test first.
Worked example: a discount
items ← 5
member ← false
IF(items ≥ 5)
{
IF(member)
{
DISPLAY("20% off")
}
ELSE
{
DISPLAY("10% off")
}
}
ELSE
{
DISPLAY("No discount")
}| Step | Test | Result |
|---|---|---|
| 1 | items ≥ 5 is 5 ≥ 5 | true: enter the first block |
| 2 | member | false: run the inner ELSE block |
| 3 | Display | 10% off |
Reading tip: find which block each ELSE belongs to by following the braces (or, in Python, the indentation). An ELSE belongs to the IF that closes just before it.
Common mistakes
- Testing the inner condition when the outer one was false. If the outer block is skipped, everything inside it is skipped too.
- Running both branches. At each IF/ELSE, only one block runs.
- Matching an ELSE with the wrong IF. Follow the braces or the indentation.
- Putting the tests in the wrong order. In a ladder, the first true test wins, and the rest are never looked at.
- Mixing up
>and≥at the edges. A score of exactly 90 is an A only if the test isscore ≥ 90.
On the exam
You are given nested code and values, and asked what is displayed. Work from the outside in. Test the outer condition, pick one block, and only then look at the conditionals inside that block. Questions also ask which values make a certain message appear, and the edge values are favourites.
Key words
Nested conditional: an IF or IF/ELSE placed inside the block of another one.
Ladder: nested conditionals where each next test sits inside the ELSE of the one before.