DATA, READ & RESTORE
DATA statements store constant values inside the source; READ pulls them out in order.
17.1 Embedding data in the program
DATA statements store constant values inside the source; READ pulls them out in order.
Strings in DATA need quotes only if they contain commas, colons, or leading/trailing spaces.
Reading past the last item causes error 4, Out of DATA.
A type mismatch (reading text into a numeric variable) raises a Syntax error at the DATA line.
FOR i = 1 TO 5
READ planet$, distance
PRINT USING "\ \ ###.## AU"; planet$; distance
NEXT
END
DATA Mercury, 0.39
DATA Venus, 0.72
DATA Earth, 1.00
DATA Mars, 1.52
DATA "Jupiter", 5.2017.2 Sentinel values and RESTORE with labels
RESTORE with no label resets to the first DATA statement in the program.
Classic BASIC: identical READ/DATA/RESTORE, but RESTORE usually could not target a specific line (GW-BASIC allows RESTORE linenumber). Machine-code loaders were often hundreds of lines of DATA numbers POKEd into memory. FreeBASIC: Data, Read, Restore label work in all dialects. VB.NET: no DATA; use array initializers, resources, JSON/CSV files, or collection initializers: Dim planets = New Dictionary(Of String, Double) From {{"Earth", 1.0}, {"Mars", 1.52}}.
RESTORE Fruits
DO
READ item$
IF item$ = "END" THEN EXIT DO
PRINT item$
LOOP
RESTORE Colors ' jump the data pointer to another label
READ n
FOR i = 1 TO n
READ c$: PRINT c$;
NEXT
END
Fruits:
DATA apple, banana, cherry, END
Colors:
DATA 3, red, green, blue