DATA
Declares lists of literal values within the program
Implemented by: dartmouth1, dartmouth4, ansimin, ansifull, hptsb, decbasic, Altair, Applesoft, TI994, CBM, Atari, atxl, msatari, trs80, level2, trscolor, colormc, spectrum, BBC, GWBASIC, MSX, bwbasic, bw32, ABasiC, QBasic, VBDOS, FreeBASIC, minibasicjs
With variations: tasc (numeric strings can be READ into a numeric variable), EndBASIC (strings must be quoted)
Also written as:
- D. in some Tiny BASIC implementations, as TRS-80 Level I BASIC
- DAT. in PDP-8E BASIC
Usage
DATA is the place to put numbers and sometimes strings which do not change each time the program is run, but you would like it to be easy to change them if necessary, as put by Altair BASIC's manual. Some examples of usage might be:
- Default configuration values of an application
- Sprites, icons and characters bitmap definitions
DATA lines are usually grouped at the bottom end of a program or routine. Each line contain a number of individual values separated by commas. Such values are retrieved and written into one or more variables sequentially by a READ statement usually placed within a loop somewhere else in the program.
5 EXAMPLE FROM ALTAIR BASIC MANUAL 10 PRINT "GUESS A NUMBER"; 20 INPUT G 30 READ D 40 IF D=-999999 THEN 90 50 IF D<>G THEN 30 60 PRINT "YOU ARE CORRECT!" 70 END 90 PRINT "BAD GUESS. TRY AGAIN." 95 RESTORE 100 GOTO 10 110 DATA 1,393,-39,28,391,-8,0,3.14,90 120 DATA 89,5,10,15,-34,-999999
The RESTORE keyword is used to make the next READ start from the first value or from a given DATA line, instead of the value(s) after the last retrieved one(s).
DATA must be the first statement of a program line in order to be found in most if not all BASICs. And the PC-BASIC (a modern implementation of GWBASIC) manual puts it clearly that a properly written DATA statement will be found and read no matter where it is within a conditional section of a program — in other words, do not expect the DATA not to be read according to the result of an IF statement.
Variations
Most BASICs also allow strings to be used as constant values in DATA statements, but following rules apply:
Comments
DATA was the primary (well, only) method of getting values into a program for processing in the initial Dartmouth implementations of BASIC, before the INPUT keyword was implemented. It certainly looked more natural back then, when batch processing of data in punched cards were common.
DATA and READ are notoriously missing in the Apple II "Mini-Manual" which refers to Apples's INTEGER BASIC.
There is an evident and intriguing similarity between the DATA statement and the CSV (Comma-Separated Values) tabular data format, which, according to some sources1)2), would be roughly described for the first time in a 1972 IBM FORTRAN manual 3).