Classic Computer Magazine Archive ANTIC VOL. 5, NO. 7 / NOVEMBER 1986

Starting Out

New Owners Column

Lesson 8:
INPUT and DATA Statements

BY DAVID PLOTKIN, ANTIC CONTRIBUTING EDITOR

This series, which started in the March, 1986 issue, teaches beginners how to program in BASIC on all Atari 8-bit computers such as the 800XL and 130XE. Antic Contributing Editor David Plotkin is a chemical engineer.

Information used by a program can be divided into two major categories. Some information is built into the program, requiring nothing from the user. Other information must be provided by user responses-such as when the program asks for the user's name.

DATA STATEMENTS

Information built in by the programmer is often presented as a DATA statement, which is any line beginning with the word DATA. This tells BASIC to accept the rest of the line as written. So you can put just about anything into a DATA statement without making a syntax error. Different pieces of data are separated by commas, as in:

10 DATA 23,45,100,DAVID,ANTIC

DATA statements can load letters into strings and set the values of variables, among other things. But be careful with DATA statements. You cannot have any other statements on the line after the DATA statement. BASIC will consider additional statements (including any REM statements) as part of the DATA to be READ. This will cause an error when the program is RUN.

The READ statement gets the information which has been stored in the DATA statement:

10 READ A:PRINT A
20 DATA 100


The most
straightforward
way a program
requests
information is
with INPUT


This places the value 100 into the variable A and prints it. You can READ multiple values at once by separating the variables in the READ statement with commas:

10 DIM D$(10):REM dimension a string
20 READ A,B,D$:DATA 100,200,DAVID

This puts the two numeric values into A and B and the word "DAVID" into string D$. It's perfectly acceptable to put the READ and DATA statements on the same line as long as no statements follow the DATA statement.

But how does the program know which value in the DATA statement goes into which variable in the READ statement? Normally, the first variable encountered in a READ statement gets the first value encountered in a DATA statement, the second variable gets the second value, and so on. The order the statements themselves are in really doesn't matter:

10 DATA 100,200
20 READ A:DATA 300,400
30 READ B,C:DATA 500
40 READ D,E

The variable A is assigned the value of 100, B gets 200, C gets 300 and so on.

But sometimes you don't want the program to assign values in strict order. One example is when you want to use the same group of DATA statements twice, perhaps to assign the same values to two sets of variables.

Another example is when you are READing and assigning values to variables in subroutines, which often are not executed in the order in which they appear in a program. The line in the subroutine which READs the DATA will be executed, but that DATA will be whatever happens to be the next line of DATA.

It is not often possible to predict when a READ statement in a subroutine will be executed, since it could result from an IF/THEN statement. The RESTORE statement can be used to tell the program which line of DATA is to be READ next:

10 READ A,B:RESTORE 30:READ C
20 DATA 100,200,300
30 DATA 1000

The values of A and B will be 100 and 200 from line 20. But the value of C will be 1000 from line 30, since the program was told to READ the next value from line 30 by the RESTORE 30 before READ C was executed. Note: RESTOREing a line which was already READ will cause the line to be READ again.

If there is any doubt about where a value will be READ from, it's best to use RESTORE. The line number in the RESTORE statement does not need to be a DATA line. If you RESTORE a line which does not contain DATA, the next line from which DATA is read will be the next line after the RESTOREd line which does contain DATA:

10 RESTORE 100
100 READ A
200 DATA 250

The DATA will be READ from line 200.

There are several things to be careful about when READing multiple variables in a program. The first and most obvious is not to run out of DATA. Trying to READ more variables than there are values in DATA statements causes an error.

RESTOREing lines to use them more than once can help avoid this problem. However, if you use RESTORE to skip some lines of DATA, you may reach the last DATA statement too soon. The program will not go back and use the skipped lines.

Another thing to avoid is mismatching your variable. If the program expects a numeric variable and gets a string of letters instead, an error will result.

INPUT STATEMENTS

The most straightforward way for the program to request information from the user is with the INPUT statement. BASIC prints a question mark [?] on the screen and waits for the user to enter information from the keyboard. The program will wait until the [RETURN] key is pressed before continuing on with the next line:

10 PRINT "What is your favorite number"; :REM A message asking for input.
20 INPUT A

When you enter a number and press [RETURN], that number is assigned to A. You can also use INPUT to enter.a character string:


The OPEN
statement
numbers specify
a multitude of
available options


10 DIM NAME$(20):REM dimension the string
20 PRINT "What is your name
30 INPUT NAME$:PRINT NAME$;" is a nice name"

OPEN/CLOSE/GET

When you use INPUT as shown above, you must press [RETURN] to end the procedure. Sometimes, though, you'll want to get information without pressing [RETURN]. If you're choosing items from a menu with a single letter, you'll want the program to jump to the appropriate routine as soon as you press the key for the routine you want. This can be done, but it's a little more complex than using the INPUT statement

First you must OPEN a channel (path) to the keyboard for input. Then you need to GET each keystroke from the keyboard:

10 OPEN #1,4,0,"K:"
20 GET #1,A
30 CLOSE #1

The various numbers in the OPEN statement specify the multitude of options available. The computer is told to OPEN channel #1. The 4 means that the channel will be used for receiving information. We can ignore the zero, which is an auxiliary byte. "K:" refers to the keyboard. Issuing the command on line 10 sets up the program to GET a single keystroke from the keyboard.

The GET command in line 20 makes the program stop and wait for a keystroke. As soon as a single key is pressed (except for the [BREAK] key), the program will continue, storing the value of the pressed key in variable A. Notice that the [RETURN] key does not need to be pressed as it does with INPUT

The CLOSE statement in line 30 CLOSEs the channel OPENed in line 20, so that the channel may be used again for other purposes later in the program.

The value of the keystroke returned in variable A could be useful to know, since the program may want to take action depending on which key was pressed. Each key has a different value.

This month's listing demonstrates the concepts presented here and also provides a program that tells you the value for each key on your keyboard. Type in Listing 1, NEWOWN7.BAS, check it with TYPO II and SAVE a copy before you RUN it. This BASIC program works on all 8-bit Atari computers of any memory size, with disk or cassette.

Try pressing any keys on the keyboard. Then press combinations of keys including the [CONTROL] key and the [SHIFT] key. Press the inverse video key at the lower right corner of your keyboard, in combination with other keys. Notice the difference in the values? Experiment and learn these concepts well; they will be important in future columns.

Listing 1: NEWOWN7.BAS Download