Difference between revisions of "INPUT"
(recreated) |
m (Slight formatting changes.) |
||
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
[[Category:BASIC keywords]][[Category:BASIC I/O]] | [[Category:BASIC keywords]][[Category:BASIC I/O]] | ||
− | '''INPUT''' is a statement that takes one or more values from the user when the program is run. | + | '''INPUT''' is a statement that takes one or more values from the user when |
+ | the program is run. | ||
{| class="wikitable" | {| class="wikitable" | ||
Line 9: | Line 10: | ||
| rowspan="2" | [[BASIC metasyntax|Syntax]] | | rowspan="2" | [[BASIC metasyntax|Syntax]] | ||
| BASIC I | | BASIC I | ||
− | | <code>INPUT</code> [<code>LINE</code>] {<code>,</code>|<code>'</code>|<code>SPC</code> <numeric>|<code>TAB(</code><numeric>[<code>, | + | | <code>INPUT</code> [<code>LINE</code>] {<code>,</code>|<code>'</code>|<code>SPC</code> <numeric>|<code>TAB(</code><numeric>[<code>,</code><numeric>]<code>)</code>|<string-const>|<string-var>|<num-var>} |
|- style="vertical-align:top" | |- style="vertical-align:top" | ||
| BASIC II-V | | BASIC II-V | ||
Line 27: | Line 28: | ||
== Description == | == Description == | ||
− | <code>INPUT</code> allows a BASIC program to collect numbers or strings from the user at run time. | + | <code>INPUT</code> allows a BASIC program to collect numbers or strings from |
+ | the user at run time. It does this by waiting for the user to type one or | ||
+ | more lines of text and press the RETURN key after each one. BASIC then | ||
+ | stores values extracted from the line or lines in a list of variables named | ||
+ | by the program. | ||
− | Each time <code>INPUT</code> waits for the user to type a line, it prints a prompt. | + | Each time <code>INPUT</code> waits for the user to type a line, it prints a |
+ | prompt. By default the prompt is a question mark <code>?</code>, but the | ||
+ | program can substitute a more descriptive reminder of what the user should | ||
+ | enter next. The program can format the prompt as well (using a syntax | ||
+ | similar to <code>[[PRINT]]</code>.) | ||
− | More than one variable can be input in one <code>INPUT</code> statement. | + | More than one variable can be input in one <code>INPUT</code> statement. The |
+ | user can press RETURN between each one, or run them together with commas | ||
+ | between them. However it is considered better programming style to prompt, | ||
+ | input (and validate) the user's values one by one. | ||
=== Details === | === Details === | ||
− | The <code>INPUT</code> sequence is read from left to right. | + | The <code>INPUT</code> sequence is read from left to right. A <string-const> |
+ | is printed without a following line feed. The <code>'</code> element causes | ||
+ | a line feed, <code>SPC</code> prints a number of spaces, and | ||
+ | <code>TAB</code> repositions the cursor as it does in <code>PRINT</code>. | ||
+ | All of these suppress the default prompt. In BBC BASIC the elements | ||
+ | <code>;</code> and <code>,</code> have no positioning effect (and do not | ||
+ | 'skip' items in the input line) but they generate the default prompt if they | ||
+ | are the last element before a variable. The <code>INPUT</code> statement | ||
+ | need not include any punctuation even if multiple variables are collected. | ||
− | The user's input is read from the [[OSRDCH]] input stream via the read-line routine [[OSWORD &00]]. | + | The user's input is read from the [[OSRDCH]] input stream via the read-line |
+ | routine [[OSWORD &00]]. BASIC permits all ASCII values to be typed but the | ||
+ | control characters are intercepted as usual: the [[VDU 13|RETURN]] key | ||
+ | completes the line, the [[VDU 27|ESCAPE]] key aborts input and raises an | ||
+ | <code>Escape</code> error, [[VDU 21|CTRL U]] erases the current line and | ||
+ | [[VDU 127|DELETE]] erases one character. All other control characters carry | ||
+ | out immediate <code>[[VDU]]</code> commands and are not added to the line. | ||
+ | The maximum line length is 238 characters excluding the new line. | ||
− | <code>[[COUNT]]</code> is updated while printing the prompts and zeroed each the time the user presses RETURN. | + | <code>[[COUNT]]</code> is updated while printing the prompts and zeroed each |
+ | the time the user presses RETURN. | ||
− | The optional <code>LINE</code> keyword causes <code>INPUT</code> to treat each line of input as a string literal. | + | The optional <code>LINE</code> keyword causes <code>INPUT</code> to treat |
− | Without <code>LINE</code>, lines of input are read and parsed until all variables are assigned values. | + | each line of input as a string literal. One line of input is taken for each |
+ | named variable. A string variable is filled with the complete line as typed, | ||
+ | and a numeric variable is assigned the first numeric value in the line (as | ||
+ | per <code>[[VAL]]</code>).<br> | ||
+ | Without <code>LINE</code>, lines of input are read and parsed until all | ||
+ | variables are assigned values. Initial whitespace is skipped; if the first | ||
+ | character is then a double quote <code>"</code> then the string literal is | ||
+ | escaped and the variable assigned as above, and any text between the closing | ||
+ | double quote and the next comma or end-of-line is discarded. Otherwise the | ||
+ | text from that character to the next comma or end-of-line is used. If there | ||
+ | is another variable than parsing restarts after the comma, and if there is | ||
+ | no comma then another line of input is read in. | ||
− | Note that a custom prompt (<code>'</code>, <code>SPC</code>, <code>TAB</code>, <string-const>) divides the list of input variables, such that if an excess of items has been input they are discarded and not used to fill variables on the right hand side. | + | Note that a custom prompt (<code>'</code>, <code>SPC</code>, |
+ | <code>TAB</code>, <string-const>) divides the list of input variables, such | ||
+ | that if an excess of items has been input they are discarded and not used to | ||
+ | fill variables on the right hand side. | ||
== Examples == | == Examples == | ||
− | |||
<pre>>INPUT temp | <pre>>INPUT temp | ||
?37.5 | ?37.5 | ||
Line 84: | Line 125: | ||
</pre> | </pre> | ||
− | + | [[User:Regregex|Regregex]] 22:42, 12 November 2012 (UTC) |
Latest revision as of 17:03, 12 November 2017
INPUT is a statement that takes one or more values from the user when the program is run.
Availability | Present in all original versions of BBC BASIC. | |
Syntax | BASIC I | INPUT [LINE ] {, |' |SPC <numeric>|TAB( <numeric>[,</code><numeric>]<code>) |<string-const>|<string-var>|<num-var>}
|
BASIC II-V | INPUT [LINE ] {; |, |' |SPC <numeric>|TAB( <numeric>[, <numeric>]) |<string-const>|<string-var>|<num-var>}
| |
Token (hex) | BASIC I-V | E8 (statement)
|
Description | BASIC I-V | Prints one or more prompts and waits for the user to type a string and press RETURN after each prompt, then parses the typed string and stores the parsed values in the named variables in sequence. |
Associated keywords | COUNT , GET , INKEY , PRINT , SPC , TAB , VAL
|
Description
INPUT
allows a BASIC program to collect numbers or strings from
the user at run time. It does this by waiting for the user to type one or
more lines of text and press the RETURN key after each one. BASIC then
stores values extracted from the line or lines in a list of variables named
by the program.
Each time INPUT
waits for the user to type a line, it prints a
prompt. By default the prompt is a question mark ?
, but the
program can substitute a more descriptive reminder of what the user should
enter next. The program can format the prompt as well (using a syntax
similar to PRINT
.)
More than one variable can be input in one INPUT
statement. The
user can press RETURN between each one, or run them together with commas
between them. However it is considered better programming style to prompt,
input (and validate) the user's values one by one.
Details
The INPUT
sequence is read from left to right. A <string-const>
is printed without a following line feed. The '
element causes
a line feed, SPC
prints a number of spaces, and
TAB
repositions the cursor as it does in PRINT
.
All of these suppress the default prompt. In BBC BASIC the elements
;
and ,
have no positioning effect (and do not
'skip' items in the input line) but they generate the default prompt if they
are the last element before a variable. The INPUT
statement
need not include any punctuation even if multiple variables are collected.
The user's input is read from the OSRDCH input stream via the read-line
routine OSWORD &00. BASIC permits all ASCII values to be typed but the
control characters are intercepted as usual: the RETURN key
completes the line, the ESCAPE key aborts input and raises an
Escape
error, CTRL U erases the current line and
DELETE erases one character. All other control characters carry
out immediate VDU
commands and are not added to the line.
The maximum line length is 238 characters excluding the new line.
COUNT
is updated while printing the prompts and zeroed each
the time the user presses RETURN.
The optional LINE
keyword causes INPUT
to treat
each line of input as a string literal. One line of input is taken for each
named variable. A string variable is filled with the complete line as typed,
and a numeric variable is assigned the first numeric value in the line (as
per VAL
).
Without LINE
, lines of input are read and parsed until all
variables are assigned values. Initial whitespace is skipped; if the first
character is then a double quote "
then the string literal is
escaped and the variable assigned as above, and any text between the closing
double quote and the next comma or end-of-line is discarded. Otherwise the
text from that character to the next comma or end-of-line is used. If there
is another variable than parsing restarts after the comma, and if there is
no comma then another line of input is read in.
Note that a custom prompt ('
, SPC
,
TAB
, <string-const>) divides the list of input variables, such
that if an excess of items has been input they are discarded and not used to
fill variables on the right hand side.
Examples
>INPUT temp ?37.5 >PRINT temp 37.5 >INPUT X%,Y% ?640 ?512 >PRINT X%,Y% 640 512 >INPUT X%,Y% ?160,256 >PRINT X%,Y% 160 256 >INPUT '"X coordinate: " X%, "Y coordinate: "Y% X coordinate: 640,512 Y coordinate: 480 >PRINT X%,Y% 640 480 >INPUT A$,B$ ?"Open, sesame" doesn't work, said Aladdin. >PRINT A$,B$ Open, sesame said Aladdin. >INPUT C$,D$ ?What about "Open, sarsaparilla", suggested the genie. >PRINT C$,D$ What about "Open sarsaparilla" >INPUT LINE E$ ?"At last", he cried, "the Cave of Wonders!" >PRINT E$ "At last", he cried, "the Cave of Wonders!"
Regregex 22:42, 12 November 2012 (UTC)