Classic Computer Magazine Archive COMPUTE! ISSUE 49 / JUNE 1984 / PAGE 86


Richard Mansfield, Senior Editor

A Wall Of Loops

It takes most people a few weeks of part-time study to learn BASIC. Of course defined functions, multidimensional arrays, and other advanced techniques would not yet be understood, but after a short time, a novice programmer can accomplish a good deal with BASIC.

Nevertheless, during those first few weeks, most of us run into a wall—one of the fundamental BASIC commands is simply beyond understanding. Try as we might, some concept thoroughly resists our efforts to learn it. For me, the wall was the ON X GOTO 100,200,300 command. With furrowed brow, I came back to it again and again, trying to see how X controlled those line numbers following the GOTO.

Simple Loops

Others have said that their wall was nested loops. Let's take a look at these loops within loops. Nested loops are one of the elements of computer power and a beginning programmer must be able to use them.

Here's a simple loop:

Program 1: Simple Looping

10 FOR I = 1 TO 100

The variable I is assigned a range of 1 to 100 in line 10. It is told that it will start out being a 1 and count up to 100 during the FOR-NEXT loop. And any commands between the FOR and the NEXT will be executed each time through this loop. In other words, line 20, which prints the current value of I, will be executed 100 times.

Anything else you want done 100 times can be squeezed in between lines 10 and 30 in this program. If you want your name printed 100 times, just put in a line 11 like this:


and it, too, will be printed. It's easy to see how this might come in handy when printing labels or addresses on a printer.

Now, to make the actions in Program 1 a bit clearer, take a look at Program 2:

Program 2: Looping Without FOR-NEXT

10 I = 1
30 I = I + 1
40 IF I = 101 THEN END
50 GOTO 20

This does exactly the same thing as Program 1, but it's a bit clumsy. As you see, we can create a loop structure without using FOR-NEXT commands, but it takes up more room, takes longer to program, and runs more slowly. It's not generally the best way to set up loops, but it does help to visualize how a loop actually works.

Stuffed And Nested

Now we can try stuffing loops inside other loops. This is a technique which amplifies the power of loops. It's called nesting and the first FOR (coupled with the last NEXT) is called the outer loop:

Program 3: Nested Loops

10 FOR I = 100 TO 500 STEP 100
40 FOR J = 1 TO 10
60 FOR T = 1 TO 100

The outer loop in this program (the FOR in line 10 and the NEXT in line 90) causes the entire program to cycle five times, executing every command in lines 20-80 five times before stopping. As an aside, the STEP command in line 10 is an interesting variation on the simple I = 100 TO 500 command. Without the STEP, this program would execute 500 times. But STEP forces the I variable to add 100 to itself each time we hit the NEXT in line 90. So, instead of a series like 1,2,3,4,5,6,7 … we get the series 100,200,300,400,500, a total of five cycles through the loop.

In any case, line 20 PRINTs a blank line, line 30 PRINTs the current value of the I variable, and then we come upon the first nested loop. The J variable is given a range of 1 to 10, so everything between lines 40-80 will be performed ten times. But since this loop is nested inside the I loop (which creates five cycles of its own), the PRINTJ in line 50 will be executed 5 times 10. In other words, the value of J will be printed a total of 50 times in this program.

An even deeper loop, called the inner loop, appears between the FOR in line 60 and the NEXT in line 70. This loop is given a range of 1 to 100, but it isn't given anything to do. It just counts up to 100 and then we perform the NEXT J in line 80.

Do-Nothing Timers

That inner T loop does actually accomplish something, however. It uses up time. Such loops are often called do-nothing loops or delay loops. Their function is to slow down the computer. Sometimes this is very handy. Computers are fast. If you are having something PRINTed to the screen and it's sliding by too fast to read, insert a delay loop and give that loop whatever range suits your reading speed. Then, before allowing the program to proceed, the delay loop will count from the low up to the high number in its range.

Here is a second version of this same program, but, again, the FOR-NEXT commands are not used. If you are still unclear about how Program 3 functions, take a look at Program 4:

Program 4: Nested Loops Without FOR-NEXTs

10 I = 100
40 J = 1
60 T = 1
70 T = T + 1
80 IF T < > 100 THEN 70
90 J = J + 1
100 IF J < 11 THEN 50
110 1=1+ 100
120 IF I = 600 THEN END
130 GOTO 20

Like Program 2, Program 4 is large, clumsy, and slow. For example, it takes five times as long to execute as Program 3, its counterpart. You'll probably never write nested loops like those found in Program 4, but you can take a look at it to see how nested loops are structured.

Program 4 also illustrates various true/false types of loop exits. Line 80 means that we keep on cycling through the loop if the variable T does not yet equal 100. We exit when T= 100. Line 100 continues to cycle as long as J is less than 11. In line 120, we exit the loop (and stop the entire program, via the END command) if I equals 600.

Rules And Customs

There are several programming rules and customs you should try to observe when working with loops. In general, a programmer cannot use the same variable name for different functions or the program might make serious errors. For example, if you are writing a program to figure out your budget and you say TAXES = 15000 (for federal tax) and then use the variable name TAXES again later in the program: TAXES = 400 (meaning state tax), you will have hopelessly confused the computer. You have to use different variable names, such as FED and STATE.

The same thing applies to loops. Each different loop must have its own name FOR I/NEXT I, FOR J/NEXT J, etc. To help keep this straight, most programmers use the variable I for their outer loop, then J, then K, and so on up the alphabet. The letters I, J, K, and L are not used for normal variables, just for loops. Similarly, the variable name T is reserved for riming loops, those delay loops we mentioned above.

Also, every FOR must have a matching NEXT to close its loop, and nested loops must not interweave. You cannot have a structure like this:

10 FOR I = 1 TO 10
20 FORJ = lTO20
30 NEXT I 

lines 30 and 40 are out of order. The inner loop, the J loop here, must be closed by its NEXT before the I loop can be closed.