Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Online Courses / University Track / Computing Foundations

Module 3 · Lesson 3 · Core

Choosing a path: if, else if and else

Write a chain of conditions that chooses exactly one branch, and trace it for any input.

  • 50 minutes
  • Serves O3
  • Assumes M3.L2

Phase 1: Prepare

Step 1 Warm-up recall

From M3.L2: evaluate NOT (5 > 3).

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • describe if, else if and else (Foundation);
  • trace a chain for a given input (Core);
  • explain why the order of conditions matters (Core).

Phase 2: Teach

Step 3 A familiar example

A ticket office charges by age. The clerk does not check every rule for every person; she goes down the list and stops at the first rule that fits.

Step 4 Clear explanation

A decision structure chooses between steps. In an if, else if, else chain, the conditions are checked from top to bottom, the steps of the first true condition are run, and all later conditions are skipped. The final else runs only if no condition was true. Here is an invented price rule: under 5 is free; under 18 costs £4; under 65 costs £8; otherwise the price is £5.

age < 5freeage < 18£4age < 65£8otherwise£5
Invented rule: entry price chosen by age. The first condition that is true decides the price; the rest are skipped.
AgeWhich condition is true first?Price (£, invented)
3age < 50
17age < 184
18age < 658
65none, so otherwise5

Because a person aged 3 meets the first condition, the later conditions never run. A person aged 18 fails age < 5 and age < 18 and meets age < 65, so pays £8. A person aged 65 meets none of the three, so the otherwise branch gives £5. The order matters: if age < 65 were checked first, a three-year-old would pay £8. Exactly one branch runs, which makes chains easy to trace by hand, as in Module 1. For a language that writes these chains, see the entry on the programming language.

Step 5 Worked example (4 marks)

Trace the price rule for age 17.

  1. age < 5? 17 < 5 is false. (1 mark)
  2. age < 18? 17 < 18 is true. (1 mark)
  3. Run this branch: price £4, and skip the rest. (1 mark)
  4. Answer: £4. (1 mark)

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. In an if, else if, else chain, how many branches run?

2. The else part runs when:

3. Using the invented price rule, age 18 pays:

4. Conditions in a chain are checked in what order?

Phase 4: Close

Step 7 Recap

  • The first true condition decides; the rest are skipped.
  • Else runs only if nothing earlier was true.
  • The order of conditions changes the result.

Step 8 Exit ticket (ungraded)

Trace the price rule for ages 4, 64 and 65.

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Rewrite the chain with the senior rule first and explain which ages would be wrong.

Common mistakes

MistakeWhy it fails
Running several branchesOnly the first true branch runs.
Putting a general condition firstPut the narrowest conditions first.
Leaving out the otherwise branchSay what happens when nothing matches.

Limits. Prices and ages here are invented for teaching; real policies must be taken from the organisation concerned.