Skip to content

Working It Out

The computer works a new value out from what you give it, following a fixed order you can steer with brackets.

You have seen the computer work out 2 + 2 and show the answer. Now it works things out from values it did not have when you wrote the program, and that is where arithmetic starts to matter.

A sum from numbers you type

ASK "First number? " INTO a
ASK "Second number? " INTO b
SHOW "The total is ", a + b

Type 7, then 5:

Output
First number? 7
Second number? 5
The total is 12

You did not know the answer when you wrote that program, and you could not have. 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.

The same line gives 12 today and 109 tomorrow. Arithmetic is +, -, * for multiply and / for divide.

Why multiply is a star

Two of those look wrong, and it is worth knowing why.

Multiply is × and divide is ÷ everywhere except on a computer. So where did * and / come from? From the keyboard.

The machines these languages grew up on carried a small, fixed set of characters, and × and ÷ were not among them. Programmers used what was in front of them: *, and the slash you already write fractions with, as in 3/4. The workaround stuck, and nearly every language since has kept it.

Some sums happen before others

What does this come to?

SHOW 2 + 3 * 4
Output
14

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) * 4
Output
20

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 it’s wrong, see why

  • You get a wrong total. One of the boxes was still empty when the sum ran. Fill both of them before the line that adds them.
  • The answer is the two numbers stuck together, like 75 instead of 12. The computer joined them as text instead of adding them as numbers. Check that you asked for numbers, and that + is doing the work rather than a comma.
  • The answer is right but not the one you meant. Check the order. 2 + 3 * 4 is 14, and if you wanted 20 the brackets were missing.
  • Someone typed something that is not a number. hello + 5 is 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.
  • * and / are on the page because × and ÷ were not on the keyboard.
  • Multiply and divide happen before add and subtract, and brackets say what to work out first.

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.