The Read-Data Command - Commodore 128D User Manual

Hide thumbs Also See for 128D:
Table of Contents

Advertisement

H
I
\
I
;
I
i
n
H
The READ-DATA Command
There is another powerful way to tell the computer what numbers or
characters to use in your program. You can use the READ statement
in your program to tell the computer to get a number or character(s)
from the DATA statement. For example, if you want the computer to
find the average of five numbers, you can use the READ and DATA
statements this way:
10T = 0
20 FOR J = 1TO5
30 READ X
40T = T + X
50 NEXT
60A = T/5
70? "AVERAGE =";A
80 END
90 DATA 5,12,1,34,18
When you run the program, the computer will print AVERAGE = 14.
The program uses the variable T to keep a running total, and calcu
lates the average in the same way as the INPUT average program.
The READ-DATA average program, however, finds the numbers to
average on a DATA line. Notice line 30, READ X. The READ com
mand tells the computer there must be a DATA statement in the pro
gram. It finds the DATA line, and uses the first number as the current
value for the variable X. The next time through the loop the second
number in the DATA statement will be used as the value for X, and
so on.
You can put any number you want in a DATA statement, but you can
not put calculations in a DATA statement. The DATA statement can
be anywhere you want in the program—even after the END state
ment. This is because the computer never really executes the DATA
statement; it just refers to it. Be sure to separate your data items
with commas, but be sure not to put a comma between the word
DATA and the first number in the list.
If you have more than one DATA statement in your program, the com
puter will refer to the one that is closest after the READ statement
being executed at the time. The computer uses a pointer to remind
itself which piece of data it read last. After the computer reads the
first number in the DATA statement, the pointer points to the second
number. When the computer comes to the READ statement again, it
assigns the second number to the variable name in the READ
statement.
59
USING C128 MODE-Advanced BASIC Programming

Advertisement

Table of Contents
loading

Table of Contents