Showing Your Work
Output isn't only fixed words. A program can show a value it worked out, and join text and values together — the first sign of a program that does more than recite.
So far the computer has shown you fixed words — whatever you typed between the quotation marks. Now it shows you something it worked out. That is the difference between a program that recites and a program that does something.
Text, or a value worked out
Look closely at these two instructions. They look almost the same, but one has quotation marks and the other doesn't:
SHOW "2 + 2" the text, exactly as written
SHOW 2 + 2 the sum, worked out
In BASIC:
10 PRINT "2 + 2"
20 PRINT 2 + 2
Run it:
This is "exactly what you say" from Unit 1, sharpened. The quotation marks tell the
computer "this is text — show it letter for letter." Without them, it reads 2 + 2
as a sum and does the arithmetic. Same characters; you chose which meaning by whether
you wrapped them in quotes. The machine isn't guessing — you told it which you meant.
Joining text and a value
Showing a bare 4 isn't much use; you usually want to say what it is. So you join
fixed text and a worked-out value into one line:
| 1 | 1 | 10 PRINT "2 + 2" | |
| 2 | 2 | 20 PRINT 2 + 2 | |
| 3 | + | 30 PRINT "Two plus two is "; 2 + 2 | |
| 3 | 4 | |
The new line says: show the text "Two plus two is ", and then, on the same line, the
value of 2 + 2. The semicolon (;) is BASIC's way of saying "and then this, joined
on". In pseudocode the idea is the same — show one thing, then another, together:
SHOW "Two plus two is ", 2 + 2
That join is how output becomes legible — Score: 100, You have 3 lives left,
Time: 42 are all a piece of fixed text with a value joined onto it. Every language
has its own glue (BASIC's is the semicolon); the idea — say what the value is, then
show it — is everywhere.
When it's wrong, see why
- You see
2 + 2instead of4. You wrapped the sum in quotation marks, so the computer showed it as text. Drop the quotes and it does the arithmetic. - You see
4with no label. No text was joined on — just the bare value. Add the fixed text and the glue (;in BASIC) to say what the4is. - The text and value run together oddly, like
Two plus two is4. The fixed text needs the space inside its quotes —"Two plus two is ", not"Two plus two is". The computer shows exactly what's between the quotes, spaces and all.
What you've learnt
- Output can show a value the computer worked out, not only fixed text.
- Quotation marks choose the meaning: in quotes it's text, shown as written; without, it's worked out. You decide which by how you write it.
- Joining fixed text to a value makes output readable — say what it is, then show it. BASIC's glue is the semicolon; every language has its own.
What's next
The computer can work a value out and show it — but the moment it has shown it, it forgets it. In Unit 4 we give the program a memory: a variable, a named place to keep a value so we can use it again. It is the most important idea in the whole course.