Module 4 - Preparation
Assignment 4.04


Objectives: After studying the Module 3 Preparation Button, you will be able to:

  1. Describe the correct syntax for READ and DATA statements.
  2. Describe the purpose of the keywords READ and DATA.
  3. Identify the correct flowchart symbol for READ.
  4. Predict the output of a simple program using READ and DATA statements.
  5. Modify a program that uses multiple LET statements so that it uses READ and DATA statements instead.

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.

Imagine that you needed to enter the name of everyone in this class...and their phone number, date of birth, address, and social security number. You could enter all of the data with INPUT or LINE INPUT, but that is a lot of data entry! But there is an even bigger problem. If you turn the computer off, you would lose all of that information and you would have to enter it all again the next time you run the program. That is very, very inefficient.

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
READ
firstname$,lastname$,age
PRINT lastname$,firstname$,age
DATA Charles,Babbage,200
END

QB is right, something seems strange about this flowchart.

  • First, the parallelogram symbol is also used for READ. That's OK because the parallelogram is the INPUT/OUTPUT symbol. It is used for READ , PRINT and INPUT.
  • Second, the data (Charles, Babbage, and 200) are not included in the flowchart. That's because there is no flowchart symbol for the DATA statement. Data would just clutter up the flowchart and programmers like neatness!
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?

  • The first thing to notice is that READ is always followed by a variable list...in this case firstname$, lastname$, and age.
  • The second important thing to notice is that DATA is always followed by a data list...in this case Charles, Babbage, and 200.

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!

  • It can't be emphasized enough, the keywords READ and DATA are a pair, so they must always be used together. If your program has a READ statement but no DATA statement, you will get an Out of Data Error.
  • READ and DATA have a specific syntax. Variables in the READ statement are separated by commas. Data in the DATA statement are also separated by commas.
  • There is a one-to-one correspondence between the variables in READ statements and the data in a DATA statements. This has two important consequences.

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.

  • If your program had five variables in READ statements, then you would need five items of data in a DATA statement.
  • If you don't have enough data items to match the variables, you will get an Out of Data Error.

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.

CLS

READ
firstname$,lastname$,age
PRINT lastname$,firstname$,age
DATA Charles,Babbage
DATA 200
END


This works just as well as the original because there are three variables and three data items and the type of variable matches up with the type of data. Thus, there is still a one-to-one correspondence. So, data can be broken up into different data statements if needed.

 


What is wrong with the following statements?

READ city$,state$,zip
DATA Miami,FL

Commas can't be used to separate variables.
There are 3 variables, but only 2 data items.
There are 2 variables, but 3 data items.
There is nothing wrong.

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).

  • Consequently, the order of data in the DATA statement has to match the "type" of variable in the READ statement.
  • So, the first item of data (Charles) is alphanumeric, the second item of data (Babbage) is alphanumeric, and the third item of data (200) is numeric.
  • If the "type" of data does not match the "type" of variable, you will get a Syntax Error.

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

There is no one-to-one correspondence between READ and DATA.
There should be three separate DATA statements.
The "types" don't match because zip$ is a string variable and 20002 is numeric data.
Nothing is wrong.

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.

  1. Describe the correct syntax for READ and DATA statements.
  2. Describe the purpose of the keywords READ and DATA.
  3. Identify the correct flowchart symbol for READ.
  4. Predict the output of a simple program using READ and DATA statements.
  5. Modify a program that uses multiple LET statements so that it uses READ and DATA statements instead.

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.

previousnext