Skip to content

Showing Your Work

Output is not only fixed words. A program can show a value it worked out, and join text and values together.

  • Foundations

SHOW displays something. We use teaching notation to reason about the result, without installing a language or choosing a machine. Quotation marks enclose text; + adds numbers. Now compare displaying a written sum with displaying the value that sum produces.

Text, or a value

Look at these two instructions. They are almost the same. One has quotation marks and one does not.

Code
SHOW "2 + 2"
SHOW 2 + 2
Output
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.

What does SHOW "3 + 4" display? What changes if we remove the quotation marks?

Show the explanation

With quotes, the output is the text 3 + 4, including its spaces and plus sign. The quotes themselves do not appear. Without quotes, SHOW 3 + 4 calculates the sum and displays 7. Both instructions are valid; which one is useful depends on whether we wanted the written question or its answer.

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:

Code
SHOW "Two plus two is ", 2 + 2
Output
Two plus two is 4

In this notation, 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.

We want Score: 5, but wrote SHOW "Score:", 2 + 3. What appears, and how would you repair it?

Show the explanation

It displays Score:5. The comma adds no space, and the quoted text has none after the colon. Put one space inside the quotes, after the colon:

Code
SHOW "Score: ", 2 + 3

Moving spaces around the comma outside the quotes does not add them to the displayed text. The sum still produces 5; we repaired the presentation, not the calculation.

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.

Take the idea to a language

The idea transfers; the punctuation needs checking. In Sinclair BASIC, PRINT "Score: ";2+3 uses a semicolon to place the items immediately together. Its comma moves to a print zone instead; it is not our SHOW comma.

Ask and use the answer explains a sentence built from several pieces. Read “Give the words somewhere to go” for its semicolons and quoted spaces. Running the complete story is optional; that lesson introduces its own variables and input. We will give values names in our next unit.

When it’s wrong, see why

  • You see 2 + 2 where you wanted 4. 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. What if the program needs to use that value again later? In Unit 4 we give it a name and keep it in a variable.