BASIC READ DATA RESTORE


Introduction to BASIC READ DATA RESTORE

The BASIC READ DATA RESTORE command allows you to easily set up data that you use in a program. BASIC was one of the early “High Level” programming languages. In other words, you don’t need to know much about how the computer itself operates. Anyone can program in the BASIC language. By learning just a few commands, you can write usable programs.

In this post, we’ll concentrate on the READ DATA RESTORE command. Basically, we’ll have a DATA set. The READ command reads this data. RESTORE resets the pointer back to the beginning of your data.

In this example, I’ll be using Altair Hard Disk BASIC. Most any version of BASIC supports this command, though.

Enter The Program

Be sure to type “NEW” to clear any programs you already have in memory. Next, we’ll enter the following program.

5 REM USER MUST ENTER CORRECT SPANISH WORD
10 FOR X = 1 TO 2 STEP 1
20 READ E$ : READ S$
30 PRINT "WHAT IS THE SPANISH WORD FOR A MALE " E$;
40 INPUT A$
50 IF A$ <> S$ THEN PRINT "TRY AGAIN" : GOTO 30
60 PRINT "CORRECT!!" : NEXT X
70 RESTORE
80 GOTO 10
100 DATA CAT,GATO,DOG,PERRO

We have a very simple program to demonstrate how the BASIC READ DATA RESTORE command works. Basically, this is a “flash card” program. The program prints an English word to the screen. At this point, the user must enter the correct word in Spanish.

Interpreting the Code

Reading and Comparing the DATA

Line #5 is simply a REMark statement. The purpose of this is to add documentation to your project.

Line #10 contains a FOR NEXT loop. In this example, the loop will execute twice. The variable X is initially set to 1. When the interpreter sees “Next X”, it will jump back to the FOR statement. The X variable increments by the STEP value until X=2. Since 2 is our terminal value, “Next X” will not return to the FOR statement when it executes. The program continues to the next line.

Line #20 reads two values from our DATA set. The first time the loop executes, the E$ contains the value “CAT”. We use the $ to indicate a text string vs. a numeric value. Likewise, S$ contains the value “GATO”

Line #30 simply prints the question to the display using the value contained in E$. The first time the program runs, this value is “CAT”. Notice the semicolon. This means the next character will appear immediately after the print statement vs. going to the next line.

Line #40 will print a ? to the display, and wait for user input. Whatever text the user enters will store to A$.

Line #50 checks the answer. If the answer does not equal (<>) what we expect, then we print “Try Again” to the display. At this point, the program goes back to Line #30. If the answer is correct, then we print the result to the screen. At this point, the program returns back to the FOR statement. After that, Line #20 reads the next two values from the data set.

RESTORE Command

After we read all of the data, we need to execute the RESTORE command. We do this in Line #70. If we try to read past the amount of data we have, the user will get an error. After we restore the data, we simply start the program again in line #80

Summary

In short, the purpose of this post was to demonstrate the READ DATA RESTORE command in the BASIC programming language. Although there are much better ways to write the code, I wanted to keep it simple, so we can concentrate on how this command works. In reality, you would want a much larger data set. Additionally, you will want a way for the user to exit the program. Don’t forget to save your program by typing SAVE “FILENAME”!

— Ricky Bryce

Leave a comment

Your email address will not be published. Required fields are marked *