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

Term 1 check-in

Week 11 · Review: Term 1 · 3 lessons
1

Read and trace pseudocode

Computing 8: Topics 1.1–1.6, pp. 9–136
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.

Worked examples
Pseudocode
1CONSTANT HOURLY = 6
2INPUT hours
3INPUT helmet
4cost ← hours * HOURLY
5IF helmet = "yes" THEN
6    cost ← cost + 2
7ENDIF
8OUTPUT "Pay ", cost
Output
Pay 20
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 typedOutput
60Collect today
20Collect this week
19Collect this month
5Collect this month
4Not 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.
DayTicketsWith bracketsWithout brackets
Sat2Full priceFamily offer
Sun5Family offerFamily offer
Mon5Full priceFull price
Mon2Full priceFull 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 ← 20
2sold ← 6
3stock ← stock - sold
4sold ← sold * 2
5stock ← stock - sold
6OUTPUT stock, " left"
2
This pseudocode has two mistakes. Find them and write it correctly.
Pseudocode
1INPUT score
2bonus = 5
3IF score >= 10 THEN
4    bonus ← 10
5OUTPUT "Bonus: ", bonus
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
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.