GoalChange a flowchart with several choices into pseudocode and back, and recognise other valid ways to write the same ELSEIF idea.
New words
nested · placed inside another structure, such as an IF inside an ELSEcase statement · a selection that compares one variable with several fixed valuesequivalent · different in how it is written but the same in what it does
Explanation
A flowchart with several choices is a row of diamonds, each on the no arrow of the one before. To write it as pseudocode, take the first diamond as IF, each diamond on a no arrow as an ELSEIF, and the last no arrow as ELSE. To go the other way, draw one diamond for each condition. The same algorithm can also be written with an IF inside an ELSE. That is called nested, and it needs an ENDIF for every IF. It does the same job, but the ELSEIF version is shorter and easier to read. Some languages have a case statement, which checks one variable against several exact values. Python has no case statement in the way this course uses it, so we use if, elif and else instead.
Worked examples
Pseudocode
1INPUT rain
2IF rain > 20 THEN
3 OUTPUT "Cancel the trip"4ELSEIF rain > 5 THEN
5 OUTPUT "Take umbrellas"6ELSEIF rain > 0 THEN
7 OUTPUT "Take light jackets"8ELSE
9 OUTPUT "Go as planned"10ENDIF
A school trip plan depends on rainfall in millimetres. The flowchart above, written as pseudocode, is shown here. For 6 mm it shows Take umbrellas, and for 0 mm it shows Go as planned.
Pseudocode
1INPUT rain
2IF rain > 20 THEN
3 OUTPUT "Cancel the trip"4ELSE
5 IF rain > 5 THEN
6 OUTPUT "Take umbrellas"7 ELSE
8 IF rain > 0 THEN
9 OUTPUT "Take light jackets"10 ELSE
11 OUTPUT "Go as planned"12 ENDIF
13 ENDIF
14ENDIF
Here is the same algorithm written with an IF inside each ELSE. Count the ENDIFs: there is one for every IF. For every input the result is the same as the ELSEIF version. For 21 mm it shows Cancel the trip.
Pseudocode
1SELECT size
2 CASE "S": OUTPUT "Small cup"3 CASE "M": OUTPUT "Medium cup"4 CASE "L": OUTPUT "Large cup"5END SELECT
Did you know? A case statement checks one variable against fixed values. This is one common way to write it, and it only works for exact matches, not for ranges such as rain > 5. Because Python has no case statement here, a programmer writes an if, elif chain. We do not run this one.
Class activity
Chart relay (no computer). Work in a team of three. Person A draws a flowchart with three diamonds, for example about what to wear for a given temperature. Person B writes it as pseudocode with IF, ELSEIF and ELSE, and Person C traces three test numbers through both and checks that they agree. Rotate the jobs and do it again with a new idea. Then rewrite one pseudocode with IF inside ELSE (nested) and decide which version is easier to read, and why. If you have a computer or tablet, draw the flowchart with a drawing program.
Practice
1
Write the flowchart as pseudocode. Use IF, ELSEIF and ELSE.
INPUT depth / IF depth > 40 THEN / OUTPUT "Stay out" / ELSEIF depth > 15 THEN / OUTPUT "Paddle only" / ELSE / OUTPUT "Safe to splash" / ENDIF. For 15 it shows Safe to splash, and for 16 it shows Paddle only.
2
Draw a flowchart for this algorithm. Then trace it for the inputs 100, 60 and 10.
Pseudocode
1INPUT cost
2IF cost > 80 THEN
3 OUTPUT "Too expensive"4ELSEIF cost > 30 THEN
5 OUTPUT "Think about it"6ELSE
7 OUTPUT "Buy it"8ENDIF
Start, INPUT cost; a diamond cost > 80? with yes to OUTPUT "Too expensive"; on the no arrow a second diamond cost > 30? with yes to OUTPUT "Think about it" and no to OUTPUT "Buy it"; then Stop. The outputs are Too expensive, Think about it, Buy it.
3
Follow the nested algorithm below for the inputs 4 and 25. Then write it as an ELSEIF statement.
Pseudocode
1INPUT n
2IF n > 20 THEN
3 OUTPUT "Huge"4ELSE
5 IF n > 10 THEN
6 OUTPUT "Large"7 ELSE
8 OUTPUT "Small"9 ENDIF
10ENDIF
4: n > 20 is false, n > 10 is false, so Small. 25: n > 20 is true, so Huge. As ELSEIF: IF n > 20 THEN OUTPUT "Huge" ELSEIF n > 10 THEN OUTPUT "Large" ELSE OUTPUT "Small" ENDIF.
4
Give one reason why the ELSEIF version is better than the nested version, and one job a case statement can do that a range test cannot.
ELSEIF is shorter, has fewer ENDIFs and needs less indentation, so it is easier to read. A case statement checks one variable against several exact values (such as the letters S, M and L) in a very tidy way.