The Read-Data Command - Commodore 128 System Manual

Table of Contents

Advertisement

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:
10 T=0
20 FOR J=1 TO 5
30 READ X
40
50 NEXT
60 A =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
calculates 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 command tells the computer there must be a DATA
statement in the program. 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
cannot put calculations in a DATA statement. The DATA
statement can be anywhere you want in the program—even after
the END statement. 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
computer will start READing from the first DATA statement when
the program is RUN. 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 moves to the next
number. When the computer comes to the READ statement
4-11

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

128d

Table of Contents