Basic computer language commands

Basic computer programming language

READ Statement

Purpose:

To read values from a DATA statement and assign them to variables.

Syntax:

READ list of variables

Comments:

A READ statement must always be used with a DATA statement.

READ statements assign variables to DATA statement values on a one-to-one basis.

READ statement variables may be numeric or string, and the values read must agree with the variable types specified. If they do not agree, a "Syntax error" results.

A single READ statement may access one or more DATA statements. They are accessed in order. Several READ statements may access the same DATA statement.

If the number of variables in list of variables exceeds the number of elements in the DATA statement(s), an "Out of data" message is printed.

If the number of variables specified is fewer than the number of elements in the DATA statement(s), subsequent READ statements begin reading data at the first unread element. If there are no subsequent READ statements, the extra data is ignored.

To reread DATA statements from the start, use the RESTORE statement.

Examples:

.
.
.
80 FOR I=1 TO 10
90 READ A(I)
100 NEXT I
110 DATA 3.08, 5.19, 3.12, 3.98, 4.24
120 DATA 5.08, 5.55, 4.00, 3.16, 3.37
.
.
.

This program segment reads the values from the DATA statements into array A. After execution, the value of A(1) is 3.08, and so on. The DATA statement (lines 110-120) may be placed anywhere in the program; they may even be placed ahead of the READ statement.

5 PRINT
10 PRINT "CITY", "STATE", "ZIP"
20 READ C$, S$, Z
30 DATA "DENVER,", "COLORADO", 80211
40 PRINT C$, S$, Z
RUN
 CITY STATE ZIP
 DENVER, COLORADO 80211

This program reads string and numeric data from the DATA statement in line 30.

BASIC commands Main Menu

Index