Let's look at DATA and READ – what do these statements do and how do you use them? Using DATA statements in a program can save memory and may be more efficient than using many equivalent lines of code. However, a DATA statement can be more difficult to decode or understand because it can look like just a random group of numbers.

DATA statements are used in conjunction with READ statements. Together they assign numbers or strings to variable names.

100 READ N110 DATA 5 is equivalent to 100 N =5

The DATA-READ concept becomes efficient when you assign several values to a variable name for a particular procedure. Let's look at an example:

When the program comes to READ A, the computer looks for the first DATA statement and assigns the first value, 3, to the variable A. The program continues, then comes to the statement READ A again. The computer has already read the first number, so it assigns the very next number, 2, to A. The process continues. Each time a READ statement is encountered, the very next data item in the DATA list is read, whether it is in the same DATA statement or the next DATA statement.

DATA Varieties

DATA statements may be placed anywhere in the program. They are ignored until a READ statement is executed. A "marker" is remembered by the computer so it knows exactly which data item has most recently been READ – and therefore which item the next READ statement will act upon.

A DATA statement may contain one item only or several items separated by commas. Data items may be numeric constants (numbers) or strings. Numbers may be positive or negative and may contain a decimal. Numbers may not be variable names and may not contain operators (such as 5/3). String variables do not need to be in quote marks unless there are leading spaces, trailing spaces, or embedded commas as part of the string. You may specify a null string by " ", or,, in a series. Example:

You may combine numbers and strings in the same DATA statements, but you must be careful that the data items in order match the READ statements. If the READ statement specifies a numeric variable, a string will not be accepted. You must have at least as many data items as the READ statements will try to access (or you will get an OUT OF DATA error). If you happen to have extra data items, they will be ignored.

A READ statement may specify one or several items. The items may be a combination of numeric and string variables. Keep in mind that READ statements only read the data and assign values to variables – later program lines would actually print, calculate, sort, or manipulate the data.

To help conserve memory, a DATA statement can be up to four screen lines long (112 characters). You can edit and insert to make the line even longer. One exception is that if you have quite a few items separated by a lot of commas, the computer will accept only a little over three lines.

Printing Lowercase As An Example

The following program illustrates how DATA and READ statements are used to save memory in defining graphics characters. To specify each character number and definition in a separate CALL CHAR statement would require 26 statements. Using DATA and READ, four lines READ and define the graphics characters, and five data lines are used.

Program 1 redefines the small capital letters in characters 97 through 122 to graphics characters which can print actual lowercase letters. Letters with ascenders or descenders will require two letters to be printed. The chart shows which small capital letter (release the alpha lock key to print these) represents which graphic character. Lines 200-300 in the program illustrate how to print the lowercase letters.

Now let's say you want to use a DATA statement to list some numbers. First you want to add the numbers, and then you want to multiply the numbers. The list of numbers for both processes is the same. To save memory (and typing effort), the TI allows you to RESTORE data. The RESTORE statement indicates that for the very next READ statement the computer will go back to the first DATA item in the program. RESTORE resets that "marker" to zero.

RUN this sample program to see how the data items are used, then RESTOREd, then used again.

RESTORE can be very useful. TI BASIC also allows you to RESTORE to a certain line of data by specifying a line number. If you have a long program with lots of DATA statements, you can use a RESTORE n where n is a line number to make sure that each READ statement will read the correct data starting with the specified line of data.

This sample program illustrates the use of the RESTORE command. The DATA statements here contain duration factors and frequencies to be used in CALL SOUND statements. Ordinarily the first READ statement would read the first data items from the very first DATA statements. However, line 130 says to start reading the data in line 260 with the very next READ statement. Ten sounds are played; then we RESTORE 260 again so the ten sounds are repeated. Line 190 says RESTORE 240 so the data will start with line 240 for the very next READ statement.

This "Southern States" program illustrates a variety of uses of DATA and READ statements. Keep in mind that the DATA statements can go anywhere in the program and are ignored until a READ statement is executed.

Note: As you are typing in programs from listings, the most likely place for bugs (errors) is in DATA statements. Be sure you copy DATA statements carefully. Watch particularly the placement of commas. Do not accidentally put a comma at the end of a DATA statement. If your data list consists of graphics definitions, those rounded characters are zeros, not the letter O. If your program stops with a BAD VALUE message, you can PRINT some of the variable names to see if you can pinpoint which DATA statement may be causing an error.

In any case, Southern States is an educational program that draws a map of the United States. One of the Southern States is outlined, and the user must type the name of the state. If the state is correct, the user must then type the name of the capital city. States are chosen in a random order. If you get the state and the capital right, that state will not appear again. However, if you miss an answer twice, the correct answer will be given and the state will appear again.

The data in lines 270-310 defines graphics characters for the map. We're using small capital letters so they can be printed, a faster method of drawing than using CALL HCHAR or CALL VCHAR. Be sure to release the alpha lock key to type in lines 320 and 480-510.

Line 330 (RESTORE 370) is not necessary the first time through the program because the data in line 370 would be the next data anyway. However, the program branches back to line 330 to RESTORE data if you'd like to try a "new" quiz. Lines 340-390 read the names of the states and the capital cities as the S$ array and C$ array.

Outlining States

Lines 540-560 randomly choose one of the states that has not previously been chosen and identified. The S$ value is set to " " (null) if the state is identified correctly. Depending on which state is chosen, certain data is RESTOREd (line 570 then lines 1500-2070).

Each state's data contains first a number representing the number of graphics characters that need to be defined. This number is READ in line 590 (READ N). Lines 600-630 then read the next data items to define the graphics characters. Line 640 reads N, the number of graphics characters that need to be placed on the map, and then lines 650-680 read the row coordinate, column coordinate, and character number from data to outline the state. To erase the state, line 1250 reads N, the number of characters needed to erase the state, and lines 1260-1290 read from the data the row coordinate, column coordinate, erasing graphic character, and number of repetitions. Most of the clearing is done with character 96, the plain yellow square, so repetitions can be used.

Program 3 Explained

Lines

110

Clear screen.

120-170

Define colors for graphics.

180-210

Print title screen.

230-310

Define graphics characters for map.

320

Define L$ for use in printing the map.

330-390

Read names of states in S$ array and corresponding capital cities in C$ array.

400-460

Print instruction screen and wait for user to press ENTER.

470-510

Clear screen and print map of United States.

520

Perform quiz for 11 states.

530

Initialize T, which keeps track of errors.

540-560

Randomly choose a state which has not previously been identified correctly.

570

Depending on state chosen, branch to appropriate RESTORE statement.

580

Clear four lines under map where answers will be typed.

590-630

Define graphics characters for particular state.

640-680

Outline state on map.

690-710

Ask for state.

720

Clear previous answer if incorrect.

730-810

Receive user's answer.

820-830

Beep then test answer.

840-940

If answer is incorrect, sound "uh-oh" and return for another answer. If answer is incorrect twice, print correct answer, wait for user to press ENTER.

If you prefer to save typing effort, you may receive a copy of Program 3 by sending $3, a blank cassette or diskette, and a stamped, self-addressed mailer to C. Regena, P.O. Box 1502, Cedar City, UT 84720. Be sure to specify "Southern States" for the TI-99/4A computer.