One Thing After Another
A program is more than one instruction — and the order you put them in is the program, every bit as much as the instructions themselves.
One instruction is a program. So is a list of them. When you give the computer several instructions, it does them in order, from the top — and that order turns out to matter as much as the instructions do.
In order, from the top
Here is a short list of instructions. In pseudocode:
SHOW "Put your socks on"
SHOW "Put your shoes on"
SHOW "Go outside"
And in BASIC:
10 PRINT "Put your socks on"
20 PRINT "Put your shoes on"
30 PRINT "Go outside"
Run it, and the three lines appear in the order you wrote them:
The computer started at the top and worked down, one instruction at a time. That is all "running a program" is: doing each instruction in turn, in the order they are given. In BASIC the line numbers set that order — 10, then 20, then 30 — which is why they climb. The idea is universal; the line numbers are BASIC's way of writing it down.
The order is the program
Now swap two of them around — give the shoes line and the socks line each other's place:
| 1 | - | 10 PRINT "Put your socks on" | |
| 2 | - | 20 PRINT "Put your shoes on" | |
| 1 | + | 10 PRINT "Put your shoes on" | |
| 2 | + | 20 PRINT "Put your socks on" | |
| 3 | 3 | 30 PRINT "Go outside" | |
| 4 | 4 | |
The instructions are exactly the same. Only their order changed. And now:
The computer didn't notice anything wrong. It can't — it has no idea what socks and shoes are. It did exactly what you said, in exactly the order you said it, the same as ever. The nonsense came from the order, and the order was yours to get right. That is the lesson hiding in a silly example: the order of the instructions is part of the program, as much as the instructions themselves. Get a real program's steps in the wrong order — check the score before you add the points, draw the player before you move them — and it will do the wrong thing just as obediently.
When it's wrong, see why
- The lines come out in the wrong order. Read them top to bottom and check that is the order you want them to happen. In BASIC, it's the line numbers that decide the order, not the order you happened to type them — so a line numbered 5 runs before one numbered 10, wherever you typed it.
- A line is missing from the output. It either has no instruction to show anything, or its number put it somewhere you didn't expect. List the program and read the numbers in order; the order you read is the order it runs.
What you've learnt
- A program is a sequence of instructions, carried out in order from the top.
- The order is part of the program. The same instructions in a different order are a different program — often a broken one.
- The computer never checks whether the order makes sense; it only follows it. Getting the order right is your job, not the machine's.
- In BASIC, the line numbers set the order — the universal idea, written the Spectrum's way.
What's next
So far the computer only shows fixed words you typed. In Unit 3 it starts showing values — numbers, sums, and text and numbers joined together — which is the first step from a program that recites to a program that works something out.