<!-- PDF page 35 -->

## Chapter 5 – READ, DATA, RESTORE

### READ, DATA and RESTORE

In some previous programs we saw that information, or data, can be entered directly into the computer using the **INPUT** statement. Sometimes this can be very tedious, especially if a lot of the data is repeated every time the program is run. You can save a lot of time by using the **READ**, **DATA** and **RESTORE** commands. For example:

```
10 READ a,b,c
20 PRINT a,b,c
30 DATA 10,20,30
```

A **READ** statement consists of **READ** followed by a list of the names of variables, separated by commas. It works rather like an **INPUT** statement, except that instead of getting you to type in the values to give to the variables, the computer looks up the values in the **DATA** statement.

Each **DATA** statement is a list of expressions – numeric or string expressions separated by commas. You can put them anywhere you like in a program, because the computer ignores them except when it is doing a **READ**. You must imagine the expressions from all the **DATA** statements in the program as being put together to form one long list of expressions, the **DATA** list. The first time the computer goes to **READ** a value, it takes the first expression from the **DATA** list; the next time, it takes the second; and thus as it meets successive **READ** statements, it works its way through the **DATA list**. (If it tries to go past the end of the **DATA** list, then it gives an error. See the next section for an easy way to avoid that)

Note that it's a waste of time putting **DATA** statements in a direct command, because **READ** will not find them. **DATA** statements have to go in the program. Let's see how these fit together in the program you've just typed in. Line 10 tells the computer to read three pieces of data and give them the variables **a**, **b** and **c**. Line 20 then says **PRINT** these variables. The **DATA** statement in line 30 gives the values of **a**, **b** and **c**. To see the order in which things work change line 20 to:

```
20 PRINT b,c,a
```

The information in **DATA** can be part of a **FOR...NEXT** loop. Type in:

```
10 FOR n=1 TO 6
20 READ d
30 DATA 2,4,6,8,10,12
40 PRINT d
50 NEXT n
```

When this program is **RUN** you can see the **READ** statement moving through the **DATA** list. **DATA** statements can also contain string variables. For example:

```
10 READ dat$
20 PRINT "The date is",dat$
30 DATA "January 1st, 2024"
40 STOP
```

This is the simple way of fetching expressions from the **DATA** list; start at the beginning and work through until you reach the end. However, you can make the computer jump about in the **DATA** list, using the **RESTORE** statement. This has **RESTORE**, followed by a line number, and makes subsequent **READ** statements start getting their data from the

