So far our examples have chosen the values in the program. Input lets us supply a value while the program runs. A greeting is a useful first example.
Read an answer into a box
ASK "What is your name? " INTO name
SHOW "Hello, ", nameThe first line shows the question and waits. In this example, press Enter
to submit one line of text into name. We keep its spaces and accept an
empty line too. The second line runs after submission and reads that stored text.
We are not checking whether it is a sensible name or converting digits to a number.
Type Sam:
What is your name? Sam Hello, Sam
Now run the same program again, and type something else:
What is your name? Ada Hello, Ada
Nothing in the program changed between those two runs; the submitted text did.
Submit Ada again and we get Hello, Ada again. Input can change a result,
but it does not promise a different result on every run.
Games use input to let players act. Their controls may read a key or a joystick rather than wait for a whole line of text like this example.
Empty is still an answer
Before Enter, the program is waiting; it has not reached the greeting. After
Enter with no text, name holds the empty text "", and the output is
"Hello, ". The quotation marks here show its boundary, including the final
space; they are not printed.
If we submit three spaces instead, those three characters are retained. The
greeting has four spaces after the comma: one from "Hello, ", plus the three
we entered. That differs from empty input even if it is hard to see on screen.
A useful name check would be extra program behaviour; this example has none.
We submit Ada twice, then an empty line. Which runs give the same greeting, and does the empty line leave the program waiting?
Show the explanation
The first two both give Hello, Ada. The empty line has been submitted, so
ASK finishes and SHOW gives "Hello, ". Waiting before Enter and accepting
empty text after Enter are different states.
When it’s wrong, see why
- The greeting runs into the name, like
Hello,Ada. The space belongs inside the quotation marks, as"Hello, ". The comma joins and adds nothing, from Unit 3. - There is no name after Hello. Check whether empty text or spaces were submitted. Both are allowed here. An empty text value is still a value.
- The computer does not know the name. Check that the box you read is the box you filled.
What you’ve learnt
- Input lets a program read what someone types and keep it in a box.
- This greeting uses submitted text without changing its instructions. Repeating the same answer gives the same greeting.
- That is the difference between watching and playing.
What’s next
You can ask for a name and greet it. But people often answer with a number, and you can add, compare and count numbers. In the next module, Working It Out, the program starts working things out from what the player types.
You can try this idea by asking for words to build a story in Meet BASIC. The lesson explains the Spectrum’s own syntax as you use it.