Tweet |
Custom Search
|
|
CHAPTER 4 MORE ON SOLVING PROBLEMS WITH BASIC READ, DATA, RESTORE, INPUT, GOTO, IF-THEN, ON-GOTO, and Loops In Chapter 3 we discussed the LET statement, which is one way of introducing data into a program. However, this can be very awkward if you want to run a program more than once with new input data. It would be better to develop generalized programs that can be run again and again with different data. This can be done with the READ and DATA statements or with the INPUT statement. With the READ and DATA statements, the data is stored in the program. With the INPUT statement, the data is entered during program execution.READ and DATA Statements Although the data is stored with the program, the READ and DATA statements allow you the flexibility to handle data independently from the steps used to solve the problem. It is much easier to change a few DATA statements each time you want to run new data, than it is to locate and change all the LET statements. This will be even easier if you adopt the recommended programming practice of placing all the DATA statements together, either at the beginning or end of the program.The DATA statement is used in conjunction with the READ statement. The data specified by DATA statements is stored in a data list in the computers memory. READ statements cause data to be read from the data list in the DATA statement and assigned the variable names specified in the READ statements.Example: When a READ statement is executed, the values in the data list are assigned consecutively to the variable names in the READ statement. The first variable name (A) in the READ statement is assigned to the first value (100) in the DATA statement, the second variable name (B) to the second value (200), and so on. At any given time, there is one value at the top of the data list and after it is used, the next one comes to the top of the list.Each READ statement causes as many values to be taken from the data list as there are variable names in the READ statement. When you are reading several variables, care should be taken to ensure that the number of variables assigned in one or more READ statements is equal to the number of values assigned in one or more DATA statements. An excess of data in DATA statements is ignored by the computer, but insufficient data will result in an error message, and the computer will halt execution of the program.Example: Data list too short When the READ statement is executed, you will get an insufficient (out of) data error message because there is no data element for variable C.Example: Data list too long When the READ statement is executed, you will NOT get an error message, rather the values 6 and 5 in the DATA statement will be ignored. All variables in READ statements, and all values in DATA statements are separated by commas. Although DATA statements can be placed anywhere in the program, it is a good programming practice to place them all together either at the beginning or end of the program. This will make them easier to find and to change when you want to run the same program using different data.If you have programmed in another language, youll notice that BASIC is different because the size of your data elements do not have to be defined. In COBOL your data elements must be defined using a PICTURE clause, and in FORTRAN they must be defined using a FORMAT statement. BASIC accepts the data elements just as they appear in the DATA statement.When you are coding DATA statements, you should ensure that the values in the DATA statements are in the same order as the variable names specified by the READ statements. DATA statements are used in the same order-as they appear in the program.Now lets see how the READ and DATA statements work in solving a problem.Suppose you have been in the "Run for your Life" program for 12 months. The first 8 months you ran 875 miles. During the next 4 months your monthly mileages were 122, 128, 125, and 118. You want to write a program that will compute and print the number of miles you ran in the 12 month period and your monthly average. A program to do this could be written this way:Note the use of multiple READ and DATA statements, lines 10 through 40. This has no significance except to show that multiple READ and DATA statements can be used. When this program is executed, the values in the DATA statements, lines 10 and 20, are assigned consecutively to the corresponding variable names in the READ statements lines 30 and 40. The number 875 is assigned to A, 122 to B, 128 to C and so on. Next the program totals the miles run in 12 months and calculates the average miles per month. It then prints a heading for total miles run followed by the total, and a heading for average miles run followed by the average. Each time you want to run the program using new data, all you have to do is change the DATA statements, in lines 10 and 20. |
||