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
2

Four ways out

Computing 8: Topic 1.2, pp. 27–58
GoalWrite and follow an IF statement with several ELSEIF parts and decide when the final ELSE is needed.
New words
range · all the values between a smallest and a largest valuefinal ELSE · the branch that catches every value that none of the conditions acceptedboundary · the value exactly on the edge between two ranges
Explanation

An IF statement can have as many ELSEIF parts as it needs. Each ELSEIF adds one more condition to test. They are good for ranges, such as ages or temperatures, where each range has its own result. Write the conditions in a sensible order, from the smallest range to the largest or the other way round. Because the earlier conditions have already failed, each ELSEIF needs to say only where its range ends. The final ELSE is optional. With ELSE, every possible input gets an answer. Without it, an input that matches no condition makes the statement do nothing. Always test the boundary values, because mistakes with < and <= hide there.

Pseudocode
1INPUT age
2IF age < 5 THEN
3    fare ← 0
4ELSEIF age <= 17 THEN
5    fare ← 2
6ELSEIF age <= 64 THEN
7    fare ← 5
8ELSE
9    fare ← 3
10ENDIF
11OUTPUT "Fare: ", fare
Worked examples
Age typedFare shown
40
52
172
185
645
653
The bus fare program above has four ranges. These are the boundary ages traced by hand. Notice that 5 is in the second range, because the first condition is age < 5, and 18 is in the third.
Pseudocode
1INPUT temp
2IF temp < 0 THEN
3    OUTPUT "Ice"
4ELSEIF temp < 10 THEN
5    OUTPUT "Cold"
6ELSEIF temp < 25 THEN
7    OUTPUT "Mild"
8ENDIF
Without an ELSE, some inputs get no answer. With 12 degrees this program shows Mild. With 30 degrees nothing is shown at all, because no condition is true and there is no ELSE. Adding ELSE with OUTPUT "Hot" would fix it.
Pseudocode
1INPUT n
2IF n > 5 THEN
3    OUTPUT "Big"
4ENDIF
5IF n > 2 THEN
6    OUTPUT "Medium"
7ENDIF
ELSEIF is not the same as several separate IF statements. With separate IFs every condition is tested. For n = 8 the first program shows two lines (Big, Medium), but the ELSEIF version shows only Big.
Class activity

Range ladder (no computer). Draw a ladder with five rungs on a long strip of paper. Write a different range of numbers on each rung, for example ‘below 10’, ‘10 to 19’, ‘20 to 29’, ‘30 to 39’, ‘40 or more’. Put a coin on the bottom rung. A partner calls a number between 0 and 60; you move the coin to the correct rung. Then write the ranges as pseudocode with IF, three ELSEIFs and an ELSE, using only < or <= tests in the right order. Check each boundary number (9, 10, 19, 20, 39, 40) on your ladder and in your pseudocode. If you have a computer or tablet, show the ladder to an adult and explain it.

Practice
1
Follow the algorithm with the hours 12, 5, 22 and 18. What is shown each time?
Pseudocode
1INPUT hour
2IF hour < 12 THEN
3    OUTPUT "Morning"
4ELSEIF hour < 18 THEN
5    OUTPUT "Afternoon"
6ELSEIF hour < 22 THEN
7    OUTPUT "Evening"
8ELSE
9    OUTPUT "Night"
10ENDIF
2
Write pseudocode for a flag at a kite festival. The user types the wind speed. Below 10 shows Green, below 25 shows Yellow, below 40 shows Orange and everything else shows Red.
3
This statement has two mistakes. Find them and write the corrected lines.
Pseudocode
1INPUT level
2IF level < 3 THEN
3    OUTPUT "Low"
4ELSEIF level < 7
5    OUTPUT "Medium"
6ELSE level >= 7 THEN
7    OUTPUT "High"
8ENDIF
4
An IF statement has one IF, three ELSEIFs and one ELSE. What is the most conditions that can be tested for one input, and for which input does this happen?