A computer can work out 2 + 2 and show 4. It can also work from values it
did not have when you wrote the program.
Here we use pseudocode: notation for explaining a program. ASK stores an answer in
a named variable, and SHOW displays a result. Type numbers for these examples.
A sum from numbers you type
ASK "First number? " INTO a
ASK "Second number? " INTO b
SHOW "The total is ", a + bType 7, then 5:
First number? 7 Second number? 5 The total is 12
The program does not have to know the answer in advance. The numbers come from
whoever is typing. a + b is an instruction to work the total out from
whatever the two boxes hold at the moment that line runs.
With inputs 7 and 5, the line gives 12; with 100 and 9, it gives 109. Arithmetic is +, -, * for
multiply and / for divide.
Why multiply is a star
On paper you may write × and ÷. In this notation, * means multiply and /
means divide. Many programming languages use those characters too: they are keys
you can type on an ordinary computer keyboard. A system lesson explains the
symbols its language uses.
Some sums happen before others
What does this come to?
SHOW 2 + 3 * 414
Not 20. The computer did not work along the line from left to right. It did the multiplication first and the addition afterwards, following the same rule you were taught for sums on paper: multiply and divide go before add and subtract.
You cannot change that rule. What you can do is say exactly what you want, by putting brackets round the part that should happen first:
SHOW (2 + 3) * 420
Brackets mean work this out first. Use them even when the rule is already on your side. You will read this line again in a month, and it should not need you to remember anything.
When the operators have equal priority
Multiplication and division have equal priority here. Work them from left to right:
SHOW 20 / 5 * 2
SHOW 20 / (5 * 2)8 2
The first line divides 20 by 5, then multiplies 4 by 2. The second divides 20 by 10. Addition and subtraction also share a priority and work from left to right. Brackets let you choose a different grouping.
What will 20 - 5 - 2 show? Where would brackets make it 17?
It shows 13: first 20 - 5 is 15, then 15 - 2 is 13.
20 - (5 - 2) shows 17, because the bracketed part gives 3 first.
When it’s wrong, see why
- You get a wrong total. Inspect both input values and the expression. Was a value missing, entered wrongly or overwritten before the sum? Set both inputs before using them.
- The answer is the two numbers stuck together, like
75instead of12. The computer joined them as text instead of adding them as numbers. Check how your target language reads numbers and joins text. Here+adds numbers; commas inSHOWplace values side by side. - The answer is right but not the one you meant. Check the order.
2 + 3 * 4is 14, and if you wanted 20 the brackets were missing. - Someone typed something that is not a number.
hello + 5is not a sum anyone can do. A finished program checks the answer before trusting it; for now, type numbers.
What you’ve learnt
- The computer does arithmetic, working a new value out from ones it already has.
- It can work from values the player gives, so the same program gives different answers.
*means multiply and/means divide in this notation.- Multiply and divide happen before add and subtract, and brackets say what to work out first. Equal-priority operations work from left to right here.
What’s next
You can work a value out. In Unit 2 you do something that looks like nonsense written down as maths, and turns out to be the most useful line in any game.