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

Plans before programs

Week 1 · 1.1 Pseudocode · 3 lessons
3

Names that hold values

Computing 8: Topic 1.1, pp. 9–26
GoalChoose valid identifiers, use assignment and arithmetic operators, and follow an algorithm line by line.
New words
variable · a named place in memory that holds one value, which can changeidentifier · the name of a variableassignment · storing a value in a variable, written with an arrow ←arithmetic operator · a symbol that calculates: +, -, * or /
Explanation

A variable is a named place in memory that stores one value, and its name is called an identifier. An identifier must be one word with no spaces. It must not start with a number or a symbol, and it must not be a reserved word of the language, such as if or class in Python. Keep to one style of capital letters, because score and Score can be different names. Storing a value is called assignment. We write it with an arrow, so total ← 5 + 2 means: work out the right side, then store the answer in the variable on the left. The equals sign = is kept for comparing. The arithmetic operators are +, -, * for multiply and / for divide. To follow an algorithm, read one line at a time and write down the new value of each variable, crossing out the old one.

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide8 / 24
Worked examples
IdentifierValid?Reason
total_costyesone word, starts with a letter
2ndGoalnostarts with a number
final scorenohas a space, so it is two words
price$nocontains a symbol
classnoa reserved word in Python
Which of these are valid identifiers? Each bad one breaks a rule.
After linestampsboughtcost
112
2125
3175
417534
510534
A trace table records every variable after each line. Follow this algorithm: 1 stamps ← 12, 2 bought ← 5, 3 stamps ← stamps + bought, 4 cost ← stamps * 2, 5 stamps ← stamps - 7. Every row of the table shows the values after that line has run.
Pseudocode
1INPUT price
2INPUT tax
3total ← price + tax
4OUTPUT "You pay ", total
Output
You pay 46
Assignment can use input values. The user types 40 and then 6. The price and the tax are added and the result is stored in total.
Class activity

Cups and paper (no computer). Label three paper cups or three squares of paper a, b and c. Write each value on a small slip of paper and put it in the right cup, replacing the old slip when a value changes. Follow these lines in order: a ← 8; b ← a * 3; a ← a - 2; c ← a + b; b ← c / 5. Show your cups to a partner after each line. (Check: at the end a holds 6, b holds 6 and c holds 30.) Then invent five lines of your own for a partner to follow. If you have a computer or tablet, run the same lines in Python to check (Python uses = instead of ← and shows b as 6.0, because / always gives a real number).

Practice
1
Follow the algorithm and copy the trace table into your book. What is shown at the end?
Pseudocode
1cups ← 4
2plates ← cups * 3
3cups ← cups + 2
4plates ← plates - cups
5OUTPUT plates
2
Which of these identifiers are valid? goal_count, 3rd place, Time, class, my_age. Give a reason for each one that is not.
3
Write pseudocode that asks for the kilos of rice and the price of one kilo, and then shows the cost. What does it show for 3 kilos at a price of 4?
4
Find the mistake in this algorithm and write the corrected line.
Pseudocode
1INPUT width
2INPUT height
3width * height ← area
4OUTPUT area