INPUT — the Program Listens
Make the program wait for the person at the keyboard. INPUT reads what they type into a variable — text into a single-letter dollar name, numbers into a numeric one — the first time the program responds to you, not just itself.
So far the program only knows what you typed into it. INPUT changes that: it stops
and waits for the person at the keyboard, then drops what they type into a variable.
It's the moment a program stops talking to itself and starts answering you.
Milestone 1 — ask a name
10 INPUT "Your name? "; n$
20 PRINT "Hello, "; n$
INPUT "Your name? "; n$ prints the prompt at the bottom of the screen and waits. Type
a name and press Enter, and it lands in n$ — a single-letter $ name, the rule from
Unit 4. Line 20 greets whatever you gave it.
Milestone 2 — ask a number
Drop the $ and INPUT reads a number instead of text, into a numeric variable:
10 INPUT "A number? "; a
20 PRINT a; " doubled is "; a + a
INPUT "A number? "; a waits for a number and stores it in a. Line 20 uses it —
here, added to itself to double it.
The $ is the whole difference. n$ is a box for text; a is a box for a number.
INPUT fills whichever you name.
When it doesn't work
Nonsense in BASICon theINPUTline. A text variable with a long name. Reading a name needs a single-letter$box —INPUT n$, notINPUT name$.- It rejected what you typed. You typed letters into a numeric
INPUT. A number box wants a number; ask for text with a$box if you need words. - The prompt didn't show. Check the punctuation:
INPUT "prompt"; n$— the prompt is a string, then a separator, then the variable.
Before and after
You started with programs that only knew what you wrote into them, and finished with
programs that wait for the person at the keyboard and use the answer — a name greeted,
a number doubled. The idea underneath: INPUT reads what's typed into a variable —
text into a single-letter $ name, a number into a numeric one.
Try this
- Two questions. Ask for a name and a number, then print them in one sentence.
- Use the answer twice. Read a number into
a, then printaanda + a + a. - Break it. Type a word at the "A number?" prompt and watch it refuse — then give it a number.
What you've learnt
INPUTstops and waits for the keyboard, then stores what's typed in a variable.INPUT n$reads text (single-letter$name);INPUT areads a number.- A prompt goes in quotes before the variable:
INPUT "Your name? "; n$. - The program now depends on the player — different input, different result.
What's next
You can store, change, and read in values now. In Unit 6 the program starts to
decide: IF ... THEN acts only when a condition holds — and Sinclair BASIC has a few
operators of its own you'll want to know.