GoalTrace pseudocode with variables, ELSEIF chains and AND, OR, NOT, and write a short algorithm of your own.
New words
assignment · storing a value in a variable, written with an arrow: total ← 5ELSEIF chain · a set of conditions tested in order, where only the first true branch runsprecedence · the order in which NOT, AND and OR are worked out: NOT first, then AND, then ORtrace table · a table that records the value of each variable after every line
Explanation
Pseudocode is a way of planning an algorithm in structured English before you write a real program. You check it by tracing: follow it line by line and write the value of each variable in a trace table. An arrow ← stores a value in a variable, while = is only used to compare. A constant is written once at the start with CONSTANT, and its name is in capital letters. In an IF … ELSEIF … ELSE chain the conditions are tested in the order they are written, and only the first true branch runs. So the order of the tests matters. AND needs both conditions to be true, OR needs at least one, and NOT turns true into false. NOT is worked out first, then AND, then OR, and brackets are worked out before everything.
Plan in pseudocode→Trace by hand→Fix the plan→Write the real program
Bike hire. A shop charges 6 for each hour and 2 extra if the customer wants a helmet. The customer asks for 3 hours and a helmet. Follow the lines in order. The cost is first 3 × 6 = 18, and the helmet adds 2, so the output is Pay 20. The constant HOURLY is set once, so if the price changes only one line must change.
kg typed
Output
60
Collect today
20
Collect this week
19
Collect this month
5
Collect this month
4
Not yet
A recycling bank sorts bags by weight: IF kg >= 50 THEN OUTPUT "Collect today", ELSEIF kg >= 20 THEN OUTPUT "Collect this week", ELSEIF kg >= 5 THEN OUTPUT "Collect this month", ELSE OUTPUT "Not yet". The tests run from the heaviest down, so each weight reaches the right branch. 20 kg is not 50 or more, but it is 20 or more, so it stops at the second test. 19 kg fails the first two tests and reaches the third. 4 kg fails all three and reaches ELSE.
Day
Tickets
With brackets
Without brackets
Sat
2
Full price
Family offer
Sun
5
Family offer
Family offer
Mon
5
Full price
Full price
Mon
2
Full price
Full price
A cinema gives a family offer on Saturday or Sunday when 4 or more tickets are bought. The plan tests (day = "Sat" OR day = "Sun") AND tickets >= 4. Brackets change the meaning. Without brackets AND is worked out first, so a Saturday with only 2 tickets wrongly gets the offer. The bracketed version is the one that matches the rule.
Class activity
Pass the plan (no computer). Each player writes a pseudocode algorithm of four to six lines on a strip of paper: it must use INPUT, one assignment with ←, and one IF with ELSE (for example for a bus fare or a lemonade price). Fold the strip so only the algorithm shows, and pass it to the next player with a test value. The next player traces it in a table, writes the output, and unfolds the strip to check with the writer. If the two answers differ, find out whether the algorithm or the trace has the mistake. If you have a computer or tablet, an adult can help you turn one strip into Python and see if the output matches.
Practice
1
Trace this algorithm and write what it outputs. Show the value of each variable after each line.
Pseudocode
1stock ← 202sold ← 63stock ← stock - sold
4sold ← sold * 25stock ← stock - sold
6OUTPUT stock, " left"
stock: 20, then 14 (after 20 − 6). sold: 6, then 12. stock: 14, then 2 (after 14 − 12). The output is 2 left.
2
This pseudocode has two mistakes. Find them and write it correctly.
Line 2 uses = to store a value, but the arrow ← is needed: bonus ← 5. The IF has no ENDIF, so ENDIF must be added after the line bonus ← 10. Corrected: bonus ← 5, IF score >= 10 THEN, bonus ← 10, ENDIF, then OUTPUT.
3
The user types 45. What is output? Then say which output can never appear for any number, and how to fix the order.
Pseudocode
1INPUT minutes
2IF minutes >= 10 THEN
3 OUTPUT "Short"4ELSEIF minutes >= 30 THEN
5 OUTPUT "Long"6ELSE
7 OUTPUT "Tiny"8ENDIF
45 is 10 or more, so the output is Short. Long can never appear: the test minutes >= 30 can never be true when the first test is false, because every number 30 or more is already 10 or more. Put the larger boundary first: minutes >= 30, then minutes >= 10, then ELSE.
4
Write pseudocode for a team checker. The user types the number of players. If it is exactly 11 it outputs Team full. If it is less than 11 it outputs Need more. Otherwise it outputs Too many.
INPUT players / IF players = 11 THEN / OUTPUT "Team full" / ELSEIF players < 11 THEN / OUTPUT "Need more" / ELSE / OUTPUT "Too many" / ENDIF. Test with 11, 7 and 14. Another correct answer uses players > 11 as the last test.