Module 4 - Game Skills 4 |
Objectives: After studying the Module 4 Game Skills 4, you will be able to:
|
YOUR LESSON Introduction As you have seen, READ - DATA is the most efficient way to deal with sets of data within a program. And, PRINT USING and LOCATE are the best solutions for precisely controlling printing on the screen. What else can we add to the programs you have written in Module 4? How about INPUT? Remember that INPUT allows you to create interactive programs. |
|
Before you get confused about all of these programming techniques for getting data into variables, let's consider some general guidelines for when to use READ - DATA, INPUT, and LET.
Was that easier than you thought? Those seem like some pretty simple general guidelines to follow; however, there are always exceptions! Here is a big one to consider. |
|
If you had to process some kind of data (like a mailing list) for all of the students in a school, it would really clutter up a program to put all of the information in READ - DATA statements. Besides, addresses often change, so you would have to change the DATA statements in the program every time that happened. Consequently, READ - DATA would not be a good choice for such a program. This kind of situation is generally handled by storing data on a diskette or hard disk. When changes occur, the information on the storage medium is updated. When the information is needed, it is read into the program from the storage medium and used to perform the task. QBasic has the ability to read and write to storage media, but that is beyond the scope of this course. On the other hand, sometimes you might be faced with the dilemma of whether to use LET or READ - DATA in a certain situation. After all, they basically accomplish the same thing--the assignment of data to variables. What's a programmer to do? Maybe the easiest way to decide is based on the number of variables being assigned. Above, we discussed that it is appropriate to use LET when you want to assign values to a few individual variables. The key distinction here is few. If you had a program that required the assignment of "many" variables, you could either use a separate LET statement for each variable as we have been doing, or you could use a READ - DATA statement; however, it is hard to define what "many" would be. That is probably an individual decision that will be based on how much "clutter" you can stand in the program vs. how "well organized" you like your programs to be. When the "clutter factor" of using too many LET statements starts to detract from the program's organized appearance, it is probably time to switch to READ - DATA. (This is probably a good rule of thumb with regard to how well you keep your room at home picked up as well!) Let's take a look at two quick examples and then tackle a new program. |
Practice Time Remember the Bake Sale program example? The data assignment section is shown below. |
REM
Assign Data (the rest of the program would be in here) DATA
Mr. Putney,15,13,18,17 |
To continue with the discussion of exceptions, let's take a look at some ways to modify this part of the program. Even though the variables cake, pie, and cookies aren't a set of data, there really is no reason why we couldn't use READ - DATA to assign their values. The following modification shows how this could be done. |
REM
Assign Data (the rest of the program would be in here) DATA
8.25,7.75,6.25 |
You should reload the example and make these modifications yourself. The only thing to keep in mind is that the variables cake, pie, and cookies are now in the first READ statement, so their corresponding values have to be in the first DATA statement. If you don't keep them matched up (remember one-to-one correspondence), the program will not run; at some point, numeric variables would be mismatched with string data and vice versa. Ooops! |
Another possible modification is to make the Bake Sale program interactive. This could be done in several ways, but we will just consider a simple possibility. What if we asked for the price of each item to be input from the keyboard instead of fixing each price at 8.25, 7.75, and 6.25? This could be done as follows. |
REM
Assign Data (the rest of the program would be in here) DATA
Mr. Putney,15,13,18,17 |
You should also make these modifications yourself and observe how the program runs. Be sure to study both examples carefully. |
YOUR ASSIGNMENT The program in this assignment is designed to give you more experience in the following areas:
Details of this assignment are provided in the following pdf file which you should print out and carefully study.
Submit your completed assignment "Assignment 04.09" to your instructor for a grade in the Dropbox area. |
A QBasic Review
|
After you complete this assignment, return to the GameBoard and move on to Assignment 04.10. |