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.
Worked examples
Pseudocode
1INPUT kg
2IF kg <= 1 THEN
3 price ← 34ELSEIF kg <= 5 THEN
5 price ← 66ELSE
7 price ← 108ENDIF
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.
Flowchart
Pseudocode
first diamond: kg <= 1?
IF kg <= 1 THEN
second diamond, on the no arrow: kg <= 5?
ELSEIF kg <= 5 THEN
last no arrow
ELSE
where the arrows meet again
ENDIF
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 ← 64ELSEIF kg <= 1 THEN
5 price ← 36ELSE
7 price ← 108ENDIF
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
12: Fast pace. 11: Steady pace, because 11 is not at least 12 but is at least 6. 6: Steady pace, because 6 >= 6. 5: Slow start, because both conditions are false so ELSE runs.
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
It shows Steady pace, because 15 >= 6 is true and the first true condition wins. It should show Fast pace. Fix: test the highest number first, so laps >= 12 comes before laps >= 6.
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.
INPUT age / IF age <= 3 THEN fee ← 0 / ELSEIF age <= 12 THEN fee ← 2 / ELSE fee ← 5 / ENDIF / OUTPUT "Fee: ", fee. The branches are indented. Any version that gives 0, 2 and 5 for the right ages is correct.
4
When the first condition in an IF ... ELSEIF ... ELSE statement is true, is the second condition tested? Explain why this is useful.
No. Once a condition is true, the rest of the statement is skipped. This is useful because only one result is given, and the program does less work.