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 ← 04ELSEIF age <= 17 THEN
5 fare ← 26ELSEIF age <= 64 THEN
7 fare ← 58ELSE
9 fare ← 310ENDIF
11OUTPUT "Fare: ", fare
Worked examples
Age typed
Fare shown
4
0
5
2
17
2
18
5
64
5
65
3
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
12: Afternoon (12 < 12 is false, 12 < 18 is true). 5: Morning. 22: Night (22 < 22 is false, so ELSE). 18: Evening (18 < 18 is false, 18 < 22 is true).
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.
INPUT speed / IF speed < 10 THEN OUTPUT "Green" / ELSEIF speed < 25 THEN OUTPUT "Yellow" / ELSEIF speed < 40 THEN OUTPUT "Orange" / ELSE OUTPUT "Red" / ENDIF (with indented branches). A speed of 25 shows Orange and 40 shows Red.
3
This statement has two mistakes. Find them and write the corrected lines.
The ELSEIF line is missing THEN. The ELSE line should not have a condition: ELSE never has one, because it takes everything left over. Corrected: ELSEIF level < 7 THEN ... ELSE ...
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?
Four conditions can be tested (the IF and the three ELSEIFs). This happens when every condition is false, so the ELSE runs. ELSE itself has no condition to test.