RESTORE Statement In most programs, values in data lists are read and processed only once.
However, there are times when it is desirable to
re-read data within a program; you can use
the RESTORE statement
to reset the pointer to the top of the data
list. In this way, the next READ statement executed starts reading
data from the top of the data list. The first variable in the READ statement
is assigned the first value in the first DATA statement.
Example:
The first READ statement, line 20, assigns 5,10, and 15 to A, B, C respectively.
The RESTORE statement, line 30, then resets the pointer to the top
of the data list. The second READ statement, line 40, assigns the first data
element, 5, to variable W; the second, 10, to X; the third, 15, to Y; and
the fourth, 20, to Z. The
last three values in the data list, 25, 30, and 35 will not be assigned.
INPUT Statement
Another way to provide data to a program
is using the INPUT statement.
Input statements are used if data is to be supplied
during program execution. They can be placed
anywhere in a program prior to the statements that need the
data. When the computer encounters an INPUT statement during program
execution, it displays a question mark, stops, and waits for the necessary
data to be entered. Once the data has been entered, execution proceeds.
For example, if you want to supply values for the variables X and Y in
a program, you include the statement:
This statement must be executed before the first statement that is to use
either of the two variables. When this INPUT
statement is executed, the computer displays
a question mark on the terminal and waits for the values of X and Y to be
input. You then enter the appropriate two numbers separated by a comma.
Since the INPUT statement signals the need for
data with only a question mark, it is good
programming practice to precede each INPUT statement with a
PRINT statement to remind you what values are to be input and in what order.
This is particularly important in a program with
several INPUT statements. The INPUT
statement specifies one or more variable names which must be
separated by commas. There must not be a comma at the end of the state-ment.
Only variable names are used in the INPUT statement, no values may be
placed in the INPUT statement. The INPUT statement is like a READ statement
because it is a way to assign values to variable names. However, the
data is entered during program execution, rather than supplied in a DATA statement
in the program. You must specify the number of values and in what order
they are to be entered by listing one variable name for each value to be
entered. If you enter too few values the computer will respond with question
marks until the required number of values have been entered. If you enter
too many values, the excess values will be ignored.
Data provided using the INPUT statement is entered during program execu-tion
but is not saved
as part of the program once the program has been run.
INPUT statements are easy to use, interactive, and conversational. In essence,
you are carrying on a conversation with the computer during program
execution. You dont have to change any part of the program to use
new data. You might ask, "Why would anyone ever want to use READ and
DATA statements?" Suppose you had a larger program with several INPUT
statements containing many variables. One mistake in entering the data
would mean the program must be run again, reentering the data and ensuring
that it is all entered correctly. When using READ and DATA statements,
a mistake may be corrected more easily, by changing only the DATA
statement that is in error and running the program again.
The following program shows how to use the INPUT statement to enter the
values needed to compute your monthly payment on a
loan: given the annual interest rate, number
of years of the loan, and amount of the loan.
Example:
After you have entered the system command RUN, the message
will appear on your terminal. You now enter the values for each variable:
.12 is for 12% interest rate, 20 for number of years, and 15000 for the
amount of the loan, $15,000. Press the
return key to enter the values into the computer.
The program will continue execution with statement number 30, do
the calculations, and print the result, $165.16.
|
|