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

Build it yourself

Week 4 · 1.3 Creating algorithms · 3 lessons
2

Many conditions

Computing 9: Topic 1.3, pp. 46–79
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.

Pseudocode
1INPUT kilos
2IF kilos <= 2 THEN
3    OUTPUT "Quick wash"
4ELSEIF kilos <= 5 THEN
5    OUTPUT "Normal wash"
6ELSEIF kilos <= 8 THEN
7    OUTPUT "Big wash"
8ELSE
9    OUTPUT "Too heavy: remove some"
10ENDIF
Worked examples
StartINPUT kiloskilos <= 2?yesnoOUTPUT "Quick wash"kilos <= 5?yesnoOUTPUT "Normal wash"kilos <= 8?yesnoOUTPUT "Big wash"OUTPUT "Too heavy: remove some"End
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.
Pseudocode
1INPUT kilos
2IF kilos <= 8 THEN
3    OUTPUT "Big wash"
4ELSEIF kilos <= 5 THEN
5    OUTPUT "Normal wash"
6ELSEIF kilos <= 2 THEN
7    OUTPUT "Quick wash"
8ELSE
9    OUTPUT "Too heavy: remove some"
10ENDIF
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
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.
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
4
Complete the table for the algorithm in the picture.
coldrainOutput of: IF cold = "yes" OR rain = "yes" THEN OUTPUT "Take a coat" ELSE OUTPUT "No coat"
yesno?
noyes?
yesyes?
nono?