AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Which path comes first?

Week 3 · 1.2 Selection in pseudocode · 3 lessons
1

First true wins

Computing 8: Topic 1.2, pp. 27–58
GoalExplain how ELSEIF tests conditions in order and follow an algorithm that has one ELSEIF.
New words
ELSEIF · a second (or later) condition in an IF statement, tested only when the conditions before it were falseorder of testing · the conditions are tested from the top down, and the first true one decides what runsbranch · one possible path through a selection
Explanation

IF ... THEN ... ELSE gives two paths. Sometimes we need three or more. ELSEIF adds another condition between IF and ELSE. The computer tests the first condition. If it is true, those lines run, and all the other conditions are ignored. If it is false, the computer tests the next condition, and so on down the list. If every condition is false, the ELSE lines run. So the first true condition wins, and only one branch ever runs. This means the order of the conditions matters a great deal. An ELSEIF is like several IF statements in a flowchart, each placed on the no arrow of the one before.

StartINPUT kgkg <= 1?yesnoprice ← 3kg <= 5?yesnoprice ← 6price ← 10OUTPUT "Postage: ", priceEnd
Worked examples
Pseudocode
1INPUT kg
2IF kg <= 1 THEN
3    price ← 3
4ELSEIF kg <= 5 THEN
5    price ← 6
6ELSE
7    price ← 10
8ENDIF
9OUTPUT "Postage: ", price
A post office charges for parcels by weight. The conditions are tested in order. For 1 kilo the first condition is true, so it shows Postage: 3. For 4 kilos the first is false and the second is true, so it shows Postage: 6. For 8 kilos both are false, so ELSE runs and it shows Postage: 10.
FlowchartPseudocode
first diamond: kg <= 1?IF kg <= 1 THEN
second diamond, on the no arrow: kg <= 5?ELSEIF kg <= 5 THEN
last no arrowELSE
where the arrows meet againENDIF
The same flowchart is shown above as pseudocode. Every ELSEIF matches a diamond on a no arrow.
Pseudocode
1INPUT kg
2IF kg <= 5 THEN
3    price ← 6
4ELSEIF kg <= 1 THEN
5    price ← 3
6ELSE
7    price ← 10
8ENDIF
9OUTPUT "Postage: ", price
The order can break an algorithm. Here the two conditions are swapped. For 1 kilo the first condition, kg <= 5, is true, so the program shows Postage: 6 instead of Postage: 3. The second condition can never be reached, because every weight of 1 kilo or less is also 5 kilos or less, so the first condition catches it first.
Class activity

Sorting row game (no computer). Write three conditions on three sheets and lay them in a row: ‘Is the card 9 or more?’ (Big), ‘Is the card 4 or more?’ (Middle), and ELSE (Small). Use a pack of number cards. Turn a card over and carry it along the row, stopping at the first sheet whose question is true. Say the answer out loud. Try the cards 9, 6, 4 and 2. Now swap the first two sheets and try 9 and 6 again. Write down what changed and why. If you have a computer or tablet, ask an adult to run a Python version with you.

Practice
1
Follow the algorithm with the numbers of laps 12, 11, 6 and 5. What is shown each time?
Pseudocode
1INPUT laps
2IF laps >= 12 THEN
3    OUTPUT "Fast pace"
4ELSEIF laps >= 6 THEN
5    OUTPUT "Steady pace"
6ELSE
7    OUTPUT "Slow start"
8ENDIF
2
A pupil swapped the conditions. What does the program show for 15 laps, and what should it show? How would you fix it?
Pseudocode
1INPUT laps
2IF laps >= 6 THEN
3    OUTPUT "Steady pace"
4ELSEIF laps >= 12 THEN
5    OUTPUT "Fast pace"
6ELSE
7    OUTPUT "Slow start"
8ENDIF
3
Write pseudocode for an entry fee. The user types an age. Age 3 or under is Free, ages 4 to 12 pay 2, and everyone else pays 5. Show the fee.
4
When the first condition in an IF ... ELSEIF ... ELSE statement is true, is the second condition tested? Explain why this is useful.