Showing Your Work
Output is not only fixed words. A program can show a value it worked out, and join text and values together.
So far the computer has shown you words you typed between quotation marks. Now it shows you something it works out for itself.
Text, or a value
Look at these two instructions. They are almost the same. One has quotation marks and one does not.
SHOW "2 + 2"
SHOW 2 + 2
2 + 2 4
The quotation marks decide the meaning. Inside them, 2 + 2 is text, and the computer
shows it letter for letter. Outside them it is a sum, and the computer works it out.
The machine is not guessing. You chose which one you meant by how you wrote it.
Joining text to a value
A bare 4 does not tell you much. Usually you want to say what it is, so you join some
fixed text to the value:
SHOW "Two plus two is ", 2 + 2
Two plus two is 4
The comma joins one thing to the next. It adds nothing of its own: no space, no
punctuation. If you want a space before the 4, put it inside the quotation marks, the
way "Two plus two is " does above.
That join is what makes output readable. Score: 100, You have 3 lives left and
Time: 42 are all a piece of fixed text with a value joined on.
When it’s wrong, see why
- You see
2 + 2where you wanted4. The sum sits inside quotation marks, so the computer showed it as text. Take the quotes off the part you want it to work out. - You see a bare number with nothing to explain it. Join some text on to say what it is.
- The text runs into the number, like
Two plus two is4. The space belongs inside the quotation marks. The computer shows exactly what sits between them.
What you’ve learnt
- Output can show a value the computer works out, not only fixed text.
- Quotation marks choose the meaning. Inside them it is text. Outside them the computer works it out.
- Joining text to a value makes output readable, and the comma adds nothing of its own.
What’s next
The computer can work a value out and show it. The moment it has shown it, it forgets it. In Unit 4 we give the program a memory.