Chapter 6

BASin

Chapter 6

Summary

READ, DATA, RESTORE

In some previous programs we saw that information, or data, can be entered directly into the computer using the INPUT statement. Sometimes this can be very tedious, especially if a lot of the data is repeated every time the program is run. You can save a lot of time by using the READ, DATA and RESTORE commands. For example:

10 READ a,b,c
20 PRINT a,b,c
30 DATA 10,20,30
40 STOP

A READ statement consists of READ followed by a list of the names of variables, separated by commas. It works rather like an INPUT statement, except that instead of getting you to type in the values to give to the variables, the computer looks up the values in the DATA statement.

Each DATA statement is a list of expressions - numeric or string expressions separated by commas. You can put them anywhere you like in a program, because the computer ignores them except when it is doing a READ. You must imagine the expressions from all the DATA statements in the program as being put together to form one long list of expressions, the DATA list. The first time the computer goes to READ a value, it takes the first expression from the DATA list; the next time, it takes the second; and thus as it meets successive READ statements, it works its way through the DATA list. (If it tries to go past the end of the DATA list, then it gives an error.)

Note that it's a waste of time putting DATA statements in a direct command, because READ will not find them. DATA statements have to go in the program. Let's see how these fit together in the program you've just typed in. Line 10 tells the computer to read three pieces of data and give them the variables a, b and c. Line 20 then says PRINT these variables. The DATA statement in line 30 gives the values of a, b and c. Line 40 stops the program. To see the order in which things work change line 20 to:

20 PRINT b,c,a

The information in DATA can be part of a FOR . . . NEXT loop. Type in

10 FOR n=1 TO 6
20 READ D
30 DATA 2,4,6,8,10,12
40 PRINT D
50 NEXT n
60 STOP

When this program is RUN you can see the READ statement moving through the DATA list. DATA statements can also contain string variables. For example:

10 READ d$
20 PRINT "The date is",d$
30 DATA "June 1st, 1982"
40 STOP

This is the simple way of fetching expressions from the DATA list: start at the beginning and work through until you reach the end. However, you can make the computer jump about in the DATA list, using the RESTORE statement. This has RESTORE, followed by a line number, and makes subsequent READ statements start getting their data from the first DATA statement at or after the given line number. (You can miss out the line number, in which case it is as though you had typed the line number of the first line in the program.)

Try this program:

10 READ a,b
20 PRINT a,b
30 RESTORE 10
40 READ x,y,z
50 PRINT x,y,z
60 DATA 1,2,3
70 STOP

In this program the data required by line 10 made a = 1 and b = 2. The RESTORE 10 instruction reset the variables, and allowed x, y and z to be READ starting from the first number in the DATA statement. Rerun this program without line 30 and see what happens.

Chapter 7

Chapter 5