Difference between revisions of "READ"

From QB64 Wiki
Jump to navigation Jump to search
Line 36: Line 36:
  PRINT C$, S$, Z
  PRINT C$, S$, Z


CITY STATE ZIP
CITY STATE ZIP
DENVER, COLORADO 80211
DENVER, COLORADO 80211

Revision as of 16:38, 31 August 2009

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:

FOR I=1 TO 10
READ A(I)
NEXT I
DATA 3.08, 5.19, 3.12, 3.98, 4.24
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 statements may be placed anywhere in the program; they may even be placed ahead of the READ statement.

PRINT
PRINT "CITY", "STATE", "ZIP"
READ C$, S$, Z
DATA "DENVER,", "COLORADO", 80211
PRINT C$, S$, Z
CITY STATE ZIP
DENVER, COLORADO 80211