107 22 READ, DATA, RESTORE One very common way of storing a whole set of information along with the computer program is to use DATA statements. You will remember that computer programs can be stored on cassette and sets of data can be stored in the program as well. For example, it might be necessary in a program to convert the month given as a number into a name. The program below stores the names of the month as DATA .
5 REPEAT 10 PRINT "GIVE THE MONTH AS A NUMBER" 20 INPUT M 30 UNTIL M>0 AND M 40 FOR X=1 TO M 50 READ A$ 60 NEXT X 70 PRINT "THE MONTH IS ";A$ 100 DATA JANUARY,FEBRUARY,MARCH,APRIL 110 DATA MAY,JUNE,JULY,AUGUST,SEPTEMBER 120 DATA OCTOBER,NOVEMBER,DECEMBER >RUN GIVE THE MONTH AS A NUMBER ?6 THE MONTH IS JUNE Lines 10 to 30 repeat until a sensible value for M is entered – it must be between 1 and 12. In the example run a value of 6 was given to M. In this case the FOR...NEXT loop between lines 40 and 60 will repeat six times. Each time through it READ s the next piece of DATA into until finally will be left containing JUNE .
It might make it clearer if an extra line is temporarily inserted at line 55 to print out the value of and X each time through the loop. >55 PRINT A$,X > >LIST 5 REPEAT 10 PRINT "GIVE THE MONTH AS A NUMBER" 20 INPUT M 30 UNTIL M>0 AND M 40 FOR X=1 TO M 50 READ A$