Module 4 - Preparation |
Objectives: After studying the Module 3 Preparation Button, you will be able to:
|
YOUR LESSON Introduction In Module 2, you used the LET statement to assign numeric and alphanumeric information to numeric and string variables, respectively. The assignment statement is extremely important because that is how you create mathematical expressions for the computer to do calculations. However, if you want to change the numbers in the calculation, you have to actually modify the program and assign new values to the variable. That is very inefficient. In Module 3, you used the INPUT statement and the LINE INPUT statement to allow the user to interactively enter information. Interactivity makes programs more versatile and more user friendly. INPUT and LINE INPUT are important, but they also have a drawback.
INPUT and LINE INPUT were not meant to handle such situations. Wouldn't it be helpful if there was a way for the computer to read in large amounts of data that a program needed over and over again? |
||
|
QB's suggestion will work when there are only a few sets of data; however, at some point that would really clutter up the program because you can only put one LET statement on a line. Programmers love neat, concise code, so the LET statement would not be a good choice in most cases. What's the solution to this problem? |
QBasic to the rescue! A third way to provide information to a program is the READ - DATA statement. That's the focus of Module 4. In this unit, you will explore the use of READ - DATA and combine it with LET and INPUT in a variety of simple programs. |
Reading
Data into Programs The keywords READ and DATA are a pair, so they must always be used together. Examine the flowchart to the right and the small program below. Can you predict the output? CLS |
|
|
QB is right, something seems strange about this flowchart.
|
||
READ
and DATA
have a very simple syntax. Notice that the READ
statement contains a list of three variables separated by commas
and the DATA statement contains
a list of three data values separated by commas. This one-to-one
correspondence between variables and data is very, very important. Type
in the program, run it, and see if you get the output that you expect.
Was your prediction correct?
Remember, the keywords READ and DATA are a pair, so they must always be used together. When a READ statement is executed in a program, the computer immediately looks for a DATA statement to go with it. There are a few important things to know about the correct syntax of the READ and DATA combination. Pay attention because there will be a quiz!
This one-to-one correspondence between READ and DATA has two important consequences. |
The number of variables being read in with READ statements has to equal the number of data items contained in DATA statements. In the example above, there were three variables in the READ statement, so there were three items of data in the DATA statement.
Try eliminating one data item in the program and re-run it to see this error. |
However, this does not mean
that you have to have the same number of DATA
statements as there are READ statements.
Study the following example carefully.
|
||
|
What
is wrong with the following statements? READ city$,state$,zip DATA Miami,FL |
The "type" of variable and the "type" of data have to match. In the example above, the first variable is a string variable (firstname$), the second variable is a string variable (lastname$), and the third variable is a numeric variable (age).
Try rearranging the data items in the program and re-run it to see this error. |
What
is wrong with the following statements?
READ city$,state$,zip$ DATA Washington,DC,20002 |
DATA statements do not have to come immediately after READ statements. Notice where the DATA statement is placed in the short example above. Although the DATA statement can be placed anywhere in a program (as long as it comes before the END statement); programmers usually prefer to place DATA statements at the end of a program. The main reason for this is that programs can contain numerous DATA statements and it clutters up the program unless they are placed at the end. Programmers like neatness and organization! It's "good programming practice." |
Practice Time Remember the wages2.bas program that you did in Module 2? That is a good program to illustrate how READ and DATA can be used to replace separate LET statements. Go ahead and link to the practice page now. |
YOUR ASSIGNMENT If you have tried the examples in this lesson and understand how to use READ and DATA statements, you should be ready for a short Quiz. When you are ready, go to the Assessment area and take the Quiz for Assignment 04.04 Preparation. |
A Quick Review Before you move on to the next assignment, please make sure that you can do the following.
If you know how to use READ and DATA, your programs will be more efficient. |
When you have completed the Quiz, please continue to the next assignment, 04.05 Knowledge. |