home *** CD-ROM | disk | FTP | other *** search
- -----
- DATA and READ Statements
-
- In the last lesson you learned about the INPUT statement. Another way
- to tell the computer something is with |DATA~ and |READ~ statements.
-
- The DATA statement is |not~ executed. Data is taken from it when the
- computer does a READ statement. After the word DATA, you put numbers or groups
- of letters in quotes separating them with commas. This creates a DATA line.
- The |first~ time the computer comes across a READ statement it will read the
- |first~ value in the DATA line; the |second~ time, the |second~ value, and so on.
-
- Try typing in and |RUN~ning |either~ of the following two examples.
-
- |10 DATA 7,11 10 DATA "FRED","BARNEY"~
- |20 READ X 20 READ X$~
- |30 PRINT X 30 PRINT X$~
- |40 READ X 40 READ X$~
- |50 PRINT X 50 PRINT X$~
- -----
- DATA and READ Statements (continued)
-
- When the computer runs out of data, an |error~ stops the program and an
- |error message~ is displayed. Try typing in the following program.
-
- |10 DATA 10,"Twenty"~
- |20 READ X~
- |30 PRINT 1,X~
- |40 READ X$~
- |50 PRINT 2,X$~
- |60 READ X~
- |70 PRINT 3,X~
-
- Because there is no third value to read, the computer runs out of data
- and stops, giving an |error~ in line number |60~. |RUN~ it and see what happens.
- -----
- GOTO Statement
-
- The |GOTO~ statement is the simplest to understand, yet one of the most
- important statements in BASIC. It tells the computer to go to a different line
- number instead of doing the next one in order.
-
- In the following sample program, the computer will |loop~ (re-do over
- and over) lines |20~, |30~ and |40~ until it runs out of data.
- -----
- IF THEN Statement
-
- The |IF THEN~ statement gives the computer a |choice~ of what to do. |IF~ a
- condition is |true~ the computer does what follows the word |THEN~, |IF~ it is |not~
- |true~, the computer goes to the |next line~. Usually a GOTO statement will
- follow the THEN.
-
- |Relational operators~ compare two values in an IF THEN statement. The
- most common relational operators follow.
-
- |=~ equal to |<~ less than |<=~ less than or equal to
- |<>~ not equal to |>~ greater than |>=~ greater than or equal to
- -----
- IF THEN Statement (continued)
-
- By placing a unique number at the end of a DATA line, and following the
- READ statement with an IF THEN statement to test for that number, you can avoid
- the |Out of DATA~ error. This program finds the average of the numbers in a
- DATA statement, and uses |99~ as an |end of data~ signal.
- -----
- IF THEN Statement (continued)
-
- Here are a couple sample programs. One will count the numeric
- constants in a DATA statement. The other will pick the numbers greater than 50
- out of a DATA statement, and print them. In both cases if the number is |999~
- the computer will stop. Try typing in and |RUN~ning them.
-
- |10 DATA 10,69,2,45,100,74,12,999 10 DATA 10,69,2,45,100,74,12,999~
- |20 READ X 20 READ X~
- |30 IF X=999 THEN GOTO 60 20 IF X=999 THEN STOP~
- |40 LET K=K+1 40 IF X<=50 THEN GOTO 20~
- |50 GOTO 20 50 PRINT X~
- |60 PRINT K 60 GOTO 20~
- -----
- Logical Operators
-
- |Logical operators~ can make the IF THEN statement more versatile. The
- logical operators in IBM BASIC are:
-
- |AND~, |OR~, |NOT~, |XOR~, |IMP~, |EQV~
-
- The first two are pretty straight forward, and are the most widely
- used. Consult your IBM BASIC manual for truth tables and explanations of the
- other operators. The |AND~ and |OR~ operators are used when you want to test two
- conditions with an |IF THEN~ statement. Here is an example of their use in a
- program. Try typing it in and |RUN~ning it, then change the values in the DATA
- statement and |RUN~ it again.
-
- |10 DATA 5,23,17,34,29,45,999~
- |20 READ X~
- |30 IF X=999 THEN GOTO 70~
- |40 IF X<20 OR X>30 THEN LET A=A+1~
- |50 IF X>=20 AND X<=30 THEN LET B=B+1~
- |60 GOTO 20~
- |70 PRINT A;"numbers either less than 20 or more than 30 and";~
- |B;"between 20 and 30."~
- -----
- ELSE Statement
-
- Another |option~ of the IF THEN statement is the |ELSE~ statement.
- Without the ELSE, the program would |drop~ through the IF THEN and go to the
- next line when the condition of the IF THEN is false. If the word ELSE follows
- the IF THEN, it will be executed rather than dropping through to the next line.
-
- Here is the last program modified to show the ELSE statement. Type it
- in and |RUN~ it.
-
- |10 DATA 5,23,17,34,29,45,999~
- |20 READ X~
- |30 IF X=999 THEN GOTO 60~
- |40 IF X<20 OR X>30 THEN LET A=A+1 ELSE LET B=B+1~
- |50 GOTO 20~
- |60 PRINT A;"numbers either less than 20 or more than 30 and";~
- |B;"between 20 and 30."~
- -----
- Multiple Statement Lines
-
- Another useful feature of BASIC on the IBM is the use of |multiple~
- |statement~ lines. You can put two or more statements on the same line number by
- separating them with a colon (|:~). This is very helpful with IF THEN
- statements.
-
- Here is a sample program that illustrates the use of multiple statement
- lines. Type it in and |RUN~ it, then change the values in the DATA statement and
- |RUN~ it again.
-
- |10 DATA 5,23,17,34,29,45,999~
- |20 READ X~
- |30 IF X=999 THEN PRINT A;"lower than 20,";B;"between 20 and 30, and";C;~
- |"above 30.":END~
- |40 IF X<20 THEN PRINT X;"is less than 20.":LET A=A+1:GOTO 20~
- |50 IF X<30 THEN PRINT X;"is between 20 and 30.":LET B=B+1:GOTO 20~
- |60 PRINT X;"is greater than 30.":LET C=C+1:GOTO 20~
- -----
- End of Lesson Three
-
- You have reached the end of lesson three. This lesson contains some of
- the most important concepts of programming. Loops and conditionals will most
- likely make up the major portion of any program you write. Be sure you have a
- thorough understanding of this lesson before going on to lesson four.
-
- If you are using the BASIC Prof, |The PC-Prof.~
- let me know who you are! Send your name |P.O. Box 26~
- and address to: |Salina, Kansas~
- |67402-0026~
-
- If you like the Prof, include a contribution ($30 - $50 suggested) to
- help support development of additional volumes. Please copy and share the
- Prof. with other IBM P.C. users.
- -----
-