Skip to content
Unit 3 of 11 1 hr learning time

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.

27% of General Programming

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:

A Spectrum screen showing two lines: 2 + 2, then 4.
The same characters, two meanings. In quotes, `2 + 2` is text — shown exactly. Without quotes, `2 + 2` is a sum — and the computer works it out to `4`.

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:

Join text and a value on one line
+1
11 10 PRINT "2 + 2"
22 20 PRINT 2 + 2
3+ 30 PRINT "Two plus two is "; 2 + 2
34

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
A Spectrum screen showing three lines: 2 + 2, then 4, then Two plus two is 4.
Fixed text and a worked-out value, joined into one readable line — `Two plus two is 4`. The program is starting to explain itself.

That join is how output becomes legibleScore: 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 + 2 instead of 4. You wrapped the sum in quotation marks, so the computer showed it as text. Drop the quotes and it does the arithmetic.
  • You see 4 with no label. No text was joined on — just the bare value. Add the fixed text and the glue (; in BASIC) to say what the 4 is.
  • 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.