GoalWrite selections with ELSEIF and with OR and AND, and put the conditions in the right order.
New words
ELSEIF · a further condition that is only tested when all the conditions before it were falseELSE · the last part of an IF; there is only one and it runs when nothing before it was trueOR · joins two conditions; the whole condition is true if at least one part is trueAND · joins two conditions; the whole condition is true only if both parts are true
Explanation
When there are more than two outcomes, use ELSEIF. The conditions are tested in order from the top. As soon as one is true its steps run and all the others are skipped. So an ELSEIF only runs when every condition above it was false. An IF can have many ELSEIF parts, but it can only have one ELSE, and it must be last. The ELSE runs when none of the conditions was true. The order matters. For bands of numbers, test the most restrictive limit first: with ‘up to’ limits (<=) that is the smallest, with ‘more than’ limits (>) it is the largest. Then each ELSEIF needs only one limit. You can also join tests inside one condition: with OR one true part is enough, and with AND both parts must be true. To plan, list all the outcomes, decide the order, write each condition, and finish with an ELSE for everything else.
The washing machine algorithm in the picture above, drawn as a flowchart. Each ELSEIF is another diamond on the No arrow. Traced with seven loads: 1 and 2 kilos give Quick wash, 3 and 5 give Normal wash, 6 and 8 give Big wash. A load of 5 leaves at the second diamond, because 5 <= 5 is Yes, and the third diamond is never reached. Only 9 reaches the end of the No arrows and gets Too heavy: remove some.
Order matters. Here the largest limit is first. A load of 1 kilo is 8 or less, so the first condition is true and the answer is Big wash, and the other conditions are never tested. The algorithm gives the wrong answer for every load up to 8.
Pseudocode
1INPUT height
2INPUT age
3IF height < 130 OR age < 9 THEN
4 OUTPUT "Not today"5ELSE
6 OUTPUT "Enjoy the ride"7ENDIF
Joining conditions with OR. A children’s ride needs a height of at least 130 and an age of at least 9. The algorithm says Not today if either one is too low. It gives Enjoy the ride for 140 and 10, and also for 130 and 9. It gives Not today for 125 and 12, for 150 and 7, and for 120 and 5.
Class activity
Human ELSEIF ladder (no computer). Draw four steps of a ladder on the floor with chalk or paper strips and write a condition on each step, from smallest to largest, and ELSE at the top. One player holds a number card and walks up from the bottom. They stop on the first step whose condition is true and read the answer on it. Try five different numbers, including the boundary values. Then swap two steps and see how the answers go wrong. Finally, write your ladder as pseudocode with ELSEIF.
Practice
1
Follow the algorithm for the ages 4, 5, 16 and 70. What is output each time?
Pseudocode
1INPUT age
2IF age < 5 THEN
3 OUTPUT "Free"4ELSEIF age < 16 THEN
5 OUTPUT "Half price"6ELSEIF age < 65 THEN
7 OUTPUT "Full price"8ELSE
9 OUTPUT "Senior"10ENDIF
4: Free. 5: 5 < 5 is false but 5 < 16 is true, so Half price. 16: 16 < 16 is false, 16 < 65 is true, so Full price. 70: no condition is true, so Senior.
2
Write pseudocode. The user types the number of goals in a match. For 0 goals it shows Goalless. For 1 or 2 goals it shows Low scoring. For more it shows High scoring.
INPUT goals / IF goals = 0 THEN / OUTPUT "Goalless" / ELSEIF goals <= 2 THEN / OUTPUT "Low scoring" / ELSE / OUTPUT "High scoring" / ENDIF. Test: 0, 1, 2 and 3 give Goalless, Low scoring, Low scoring, High scoring.
3
A learner types 45 and gets Short, but the answer should be Long. Explain why, and correct the algorithm.
Pseudocode
1INPUT mins
2IF mins > 0 THEN
3 OUTPUT "Short"4ELSEIF mins > 30 THEN
5 OUTPUT "Long"6ELSE
7 OUTPUT "None"8ENDIF
45 > 0 is true, so the first condition runs and the second is never tested. Any number over 30 is also over 0. Put the larger limit first: IF mins > 30 THEN Long, ELSEIF mins > 0 THEN Short, ELSE None.
4
Complete the table for the algorithm in the picture.
cold
rain
Output of: IF cold = "yes" OR rain = "yes" THEN OUTPUT "Take a coat" ELSE OUTPUT "No coat"
yes
no
?
no
yes
?
yes
yes
?
no
no
?
yes, no: Take a coat (cold is yes). no, yes: Take a coat (rain is yes). yes, yes: Take a coat. no, no: No coat. OR is false only when both parts are false.