Classic Computer Magazine Archive ANTIC VOL. 5, NO. 2 / JUNE 1986

starting out

NEW OWNERS COLUMN

Lesson 4: Nested Loops

by DAVID PLOTKIN



Learn how to program BASIC on Atari 8-bit computers such as the 800XL and the 130XE. This new course for beginners started in our March, 1986 issue. Author David Plotkin, veteran Antic writer and programmer, is a chemical engineer.


Last month, we explained how to use IF/THEN statements for making decisions, and how to use FOR/NEXT statements to execute loops. This month we are going to talk some more about these two sets of commands, as well as some commands for making IF/THEN and FOR/NEXT more flexible.

The IF/THEN statement is used to test IF a condition is true, and THEN take the appropriate action. We are not limited to just testing whether two variables are equal. A whole range of tests can be performed. You may test for the following conditions:

   = Variables are equal to each other

   < First variable is less than the second variable or constant

   > First variable is greater than the second variable or constant

   <> First variable is not equal to the second variable or constant.

   >= First variable is greater than or equal to the second variable or constant

   <= First variable is less than or equal to the second variable or constant

   With this many options available, the IF/THEN statement can be used to test nearly any set of conditions. For example:

   10 IF (XXX >=4) THEN...
(greater than or equal to)

20 IF (XXX < > YYY) THEN...
(not equal to)

30 IF (XXX < 1) THEN...
(less than)
 

NESTING
Both the IF and the THEN must be contained within the same program line. But the IF/THEN statement can be nested. That is, an IF/THEN statement may contain another IF/THEN statement, up to the limit imposed by the length of the line:

   10 IF XXX=5 THEN IF YYY=6 THEN IF ZZZ = 7 THEN PRINT "ALL CORRECT"

   The words ALL CORRECT will be printed only if all three conditions are met. If any of the conditions are false-that is, if any of the variables are not equal to the number in the equation, then the rest of the statement will not be executed.

There is a more efficient way to test for multiple conditions than by using multiple IF/THEN statements-the AND and OR commands which were mentioned last month. The AND and OR commands will test multiple conditions, two at a time, to determine whether the combination of the conditions is true or false. For the AND statement, the result is true only if both statements are true.

AND "TRUTH TABLE"

Condition 1   Condition 2    Result
True          True           True
True          False          False
False         True           False
False         False          False

   An example of using the AND statement in a program might be:

   10 IF (XXX=5 AND YYY=6) THEN PRINT "BOTH CORRECT"

   The words BOTH CORRECT will print only if XXX =5 and YYY= 6 (both are true). If either one is equal to something else (one is false), then the words will not be printed.

The OR statement works somewhat differently. The result is true if either one is true:

OR "TRUTH TABLE"

Condition 1    Condition 2    Result
True           True           True
True           False          True
False          True           True
False          False          False

   An example of using OR might be:

   10 IF (XXX=5 OR YYY=6) THEN PRINT "ONE OR BOTH ARE CORRECT"

   The words ONE OR BOTH ARE CORRECT will print out unless neither variable is equal to the appropriate value. If either one is equal (one is true), then the statement will print out.

Although the AND and OR commands test conditions for true or false two at a time, they can be used to test more than two conditions. Take this example:

   10 IF(XXX=5 AND YYY=6 AND ZZZ=8 AND BBB=9) THEN PRINT "ALL CORRECT"

   This line will be evaluated by first testing whether XXX=5 AND YYY=6. If the result is true (they are both equal), then the true result will be tested against the next condition (ZZZ = 8). Each intermediate result is used as one of the two conditions to test with the next condition.

Clearly in this example, any of the statements being false will lead the whole equation to be false and the words will not be printed. AND and OR can be combined in the same statement. Again the conditions are evaluated two by two, with each intermediate result used to evaluate the next condition. Assuming that XXX=5, YYY=6 and ZZZ=7:

   10 IF (XXX=5 OR YYY=7) AND (YYY= 8 OR ZZZ = 7) THEN PRINT "WHAT A TEST!"

   This statement evaluates as true, and the words are printed. XXX is equal to 5, so the first combination (XXX =5 OR YYY= 7) is true. ZZZ is equal to 7,so the second combination (YYY= 8 OR ZZZ = 7) is also true. ANDing the two true conditions together (TRUE AND TRUE) is also true. The use of AND and OR is a very powerful tool in making program decisions, so you should be comfortable with their use. If you aren't too sure that you can determine when a statement is true or false, check this month's type-in program for a quiz of whether various complex program statements are true or false.

The final command that is useful in making decisions in a program is NOT The NOT command takes the opposite of of an evaluation's result. That is, if a statement evaluates as true, then NOT will evaluate the statement as false:

   10 IF NOT (XXX>l AND YYY=3) THEN...

   If XXX=2 and YYY=3, so that the statement (XXX>1 AND YYY= 3) is true, then the above line will not execute the part of the statement after "THEN" because NOT takes the true statement and makes it false.

FOR/NEXT NESTING
Last month I explained FOR/NEXT loops. There will be times when you want to change two or more variables at the same time. One way to do this is to nest two or more FOR/NEXT loops-place one loop inside another:

   10 FOR LOOP1 =100 TO 200

20 FOR LOOP2 = 15 TO 4 STEP -1

30 SOUND 0,LOOP1,10,LOOP2

40 NEXT LOOP2

50 NEXT LOOP1

   This example will play a type of whistle on your Atari. Let's see how this works. Line 10 initializes the first loop variable (LOOP1). Line 20 initializes the second loop variable (LOOP2). Line 30 plays the sound, using the values of LOOP1 and LOOP2 as parameters. Line 40 represents the termination of the LOOP2 loop. Finally, line 50 terminates the LOOP1 loop.

There are some important things to notice about this example. When nesting, each inner loop must reside entirely within an outer loop. In this case, the inner loop (LOOP2) starts and ends within the outer loop (LOOP1). Each time the outer loop executes once, the inner loop executes through the entire range of the variable specified in the FOR statement.

If an inner loop is not contained entirely within an outer loop, an error will result. This can happen accidentally in a program when you are doing a lot of jumping around. (GOTOs will be discussed in a future column.) When you use nested loops, you have to be careful about what occurs in the inner loop. For example, if your inner loop modifies the variable used in the outer loop, you may exit before you want to, or you may never exit at all:

   10 FOR LOOPl=0 TO 10

20 FOR LOOP2=100 TO 200:LOOP1=0:NEXT LOOP2

30 NEXT LOOP1

   This program will run forever (or until you press the [BREAK] key) because the inner loop is setting LOOP1 equal to zero each time it is executed. The outer loop will increment LOOP1 to 1, but it will never reach 10. This illustrates that special care must be taken when you are programming nested loops.

FOR/NEXT LOOP EXITS
Some of the programming commands to be covered in future columns will tell you how to jump from one program line to another during the run of the program. Obviously, such commands could be used to jump out of a FOR/NEXT loop before the variable has reached its limiting value:

   10 FOR LOOP=1 TO 10

20 GOTO 40:REM (sends the program to line 40, skipping line 30)

30 NEXT LOOP

40 REM Pick up here

   This is bad programming practice! If you leave a loop without completing it and executing the NEXT statement, your computer will not know that the loop is finished. The part of memory in your computer which keeps track of NEXT statements will gradually fill up with uncompleted NEXT statements, eventually leading to an OUT OF MEMORY error. This can happen even though you have plenty of regular memory left. There is a much better way to exit a loop without going through it as many times as the FOR statement specifies. Have the statements in the loop modify the loop variable so that it is outside the range specified in the FOR statement:

   10 FOR LOOP=1 TO 10

20 PRINT LOOP:IF LOOP>5 THEN LOOP =11

30 NEXT LOOP

   This set of statements will execute until LOOP = 6. Then the IF/THEN statement in line 20 takes over and makes LOOP =11, which is outside the range specified in line 10. Since LOOP is outside the range, the program will exit the loop and "fall through" to the program line following the NEXT LOOP statement. This method of properly exiting a loop cleans up your computer memory and doesn't lead to the problem discussed earlier.

THE LISTING
This month's listing tests whether you understand how to combine AND, OR and NOT for setting up decisions in IF/THEN statements. Type in the listing, NEWOWN4.BAS and SAVE a copy before you RUN it, following the instructions from previous lessons.

A statement will be printed on the screen, and you will enter T or F for True or False. If you get many of the answers wrong, I suggest you re-read the sections of this column dealing with this subject, because the concept of Test/Decision in the IF/THEN statement is very important to your success as a programmer.

Atari programming beginners will find additional details about topics covered by this series in Lon Poole's fine introductory book, Your Atari Computer, $17.95 from Osborne/ McGraw-Hill Publishing Berkeley, CA. -ANTIC ED

Listing 1   NEWOWN4.BAS Download