Q(uick)BASIC Statement: DATA
Quick View
DATA
A non-executable statement that stores the numeric and string constants used by a program's READ statements
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- DATA constant[,constant]…
- READ variablelist
- RESTORE [line]
Description/Parameter(s)
DATA specifies values to be read by subsequent READ statements.
READ reads those values and assigns them to variables.
RESTORE allows READ to reread values in specified DATA statements.
constant | One or more numeric or string constants specifying the data to be read. String constants containing commas, colons, or leading or trailing spaces are enclosed in quotation marks (" "). |
variablelist | One or more variables, separated by commas, that are assigned data values. Variable names can consist of up to 40 characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.). |
line | The label or line number of a DATA statement. If line is omitted, the next READ statement reads values in the first DATA statement in the program. |
- DATA statements can be entered only at the module level. They cannot be used in procedures.
Example
FOR i% = 1 TO 3
READ a%, b$
PRINT a%, b$
RESTORE
NEXT i%
DATA 1, "Repeat"
Syntax
- DATA constant[,constant]…
Description/Parameter(s)
The constant1, constant2, and so on in a DATA statement can be any valid numeric or string constant.
Names of symbolic constants (defined in a CONST statement) appearing in DATA statements are interpreted as strings, rather than names of constants. For example, in the following program fragment the second data item is a string, "PI", and not the value 3.141593:
- CONST PI=3.141593
- ⋮
- DATA 2.20, PI,45,7
- ⋮
A DATA statement may contain as many constants as will fit on a line. The constants are separated by commas.
Note: | String constants in DATA statements require double quotes only if they contain commas, colons, or significant leading or trailing spaces. |
Null data items (indicated by a missing value) can appear in a data list:
- DATA 1,2,,4,5
When a null item is read into a numeric variable, the variable has the value 0. When a null item is read into a string variable, the variable has the null string value ("").
You may use any number of DATA statements.
When working in the QuickBASIC environment, DATA statements can only be entered in the module-level code. QuickBASIC moves all DATA statements not in the module-level code to the module-level code when it reads a source file. READ statements using the DATA statements can appear anywhere in the program.
DATA statements are used in the order in which they appear in the source file. You may think of the items in several DATA statements as one continuous list of items, regardless of how many items are in a statement or where the statement appears in the program.
You may reread DATA statements by using the RESTORE statement.
Examples
The following example displays the current date by converting the date returned by the DATE$ function.
' Get the date.
C$ = DATE$
' Use VAL to split the month off the string returned by
' DATE$.
FOR I% = 1 TO VAL(C$)
READ Month$
NEXT
DATA January, February, March, April, May, June, July
DATA August, September, October, November, December
' Get the day.
Day$ = MID$(C$,4,2)
IF LEFT$(Day$,1) = "0" THEN Day$ = RIGHT$(Day$,1)
' Get the year.
Year$ = RIGHT$(C$,4)
PRINT "Today is " Month$ " " Day$ ", " Year$
Sample Output:
Today is September 21, 1988The following example shows how null data items are handled:
DATA abc,,def
DATA 1,,2
READ A$, B$, C$ 'B$ = ""
PRINT A$, B$, C$
PRINT
READ A, B, C 'B = 0
PRINT A, B, C
Sample Output:
abc def 1 0 2See also:
Syntax
- DATA constant[,constant]…
Description/Parameter(s)
- Names of symbolic constants (defined in a CONST statement) that appear in DATA statements are interpreted as strings, rather than as names of constants. For example, in the following program fragment the second data item is a string, PI, and not the value 3.141593:
- CONST PI = 3.141593
- ⋮
- DATA 2.20,PI,45,7
- ⋮
- A DATA statement can contain as many constants as will fit on a line. The constants are separated by commas.
- Null data items (indicated by a missing value) can appear in a data list:
- DATA 1,2,,4,5
- When a null item is read into a numeric variable, the variable has the value 0. When a null item is read into a string variable, the variable has the null string value ("").
- You can use any number of DATA statements.
- When working in the QBX environment, DATA statements can be entered only in the module-level code. QBX moves all DATA statements not in the module-level code to the module-level code when it reads a source file. READ statements can appear anywhere in the program.
- DATA statements are used in the order in which they appear in the source file. You may think of the items in several DATA statements as one continuous list of items, regardless of how many items are in a statement or where the statement appears in the program.
- You can reread DATA statements by using the RESTORE statement.
- REM statements appearing at the end of DATA statements must be preceded by a colon. Without a colon, BASIC interprets trailing REM statements as string data.
Examples
The following example displays the current date by converting the date returned by the DATE$ function. The names of months are stored in DATA statements and assigned to a string variable using the READ statement.
'Get the date.
C$ = DATE$
'Use VAL to find the month from the string returned by DATE$.
FOR I% = 1 TO VAL(C$)
READ Month$
NEXT
DATA January, February, March, April, May, June, July
DATA August, September, October, November, December
'Get the day.
Day$ = MID$(C$, 4, 2)
IF LEFT$(Day$, 1) = "0" THEN Day$ = RIGHT$(Day$, 1)
'Get the year.
Year$ = RIGHT$(C$, 4)
PRINT "Today is "; Month$; " "; Day$; ", "; Year$
Sample Output:
Today is September 21, 1989This example shows how null data items are handled:
RESTORE NullData
NullData:
DATA abc,,def
DATA 1,,2
READ A$, B$, C$ 'B$ = ""
PRINT A$, B$, C$
PRINT
READ A, B, C 'B = 0
PRINT A, B, C
Sample Output:
abc def 1 0 2See also: