LOOPING
by Guy Cousineau


FOR x=1 TO 10

FOR x=a TO a+b

FOR x=10 TO 5 step -1


Above are various ways of starting a loop. But what is a loop anyway? It is a series of statements which must be repeated several times. Using loops helps reduce the number of program lines; besides it would be tedious to re-write the same segment of code 20 or even thousands of times for large loops. Consider the following program:


10 FOR x = 1 TO 10

20 PRINT x

30 NEXT x


As you would suspect, the program would print the numbers 1 through 10 and then stop. X is used as a counter by the BASIC program and the NEXT X instruction returns control to line 10 until such time as X is greater than 10. But what if I want to print even numbers from 10 to 20?


10 FOR x = 10 to 20 STEP 2

20 PRINT x

30 NEXT x


Note the new STEP instruction in line 10. It tells the program to increase the value of X by 2 on each pass. The STEP instruction can be any positive or negative real number: -3.2, +3.66, .001, are all valid values, you can even use a variable! Note that the STEP instruction is optional and a default value of +1 is assumed if no STEP is given on the FOR line.

Say you want to print a multiplication table for values from 0 to 5. Here you need 2 loops, a loop NESTED inside another loop:


10 FOR x = 0 TO 5

20 FOR y = 0 TO 5

30 PRINT x*y

40 NEXT y

50 NEXT x


Note the large loop which extends from line 10 to line 50, and the smaller NESTED loop from line 20 to line 40. The second loop will run for values of Y from 0 to 5 and then fall through to the X loop which will ask to repeat the Y loop one more time.

Now for the programming tips. It is not necessary to specify the variable name when giving a NEXT command. BASIC keeps track of the CURRENT loop and will execute it automatically. Furthermore, NEXT instructions will actually run FASTER if no variable is supplied. The multiplication table routine could therefore be written as:


10 FOR x = 0 TO 5

20 FOR y = 0 TO 5

30 PRINT x*y

40 NEXT

50 NEXT


If you have several nested loops, giving umpteen NEXT instructions may get tedious and take up valuable program space. There is another alternative: put your NEXT instructions in one command:


10 FOR x = 0 TO 5

20 FOR y = 0 TO 5

30 PRINT x*y

40 NEXT y,x

This program will run just as the others, albeit a trifle slower. The instruction on line 40 means DO A NEXT Y UNTIL THERE ARE NO MORE THEN DO A NEXT X. Note that it is very important to specify the variables in the correct order.

Here's one for advanced programmers. What if you want to break out of a loop? Let's go back to the table above. Say I want to print my multiplication table just for results that are smaller than 10....


10 FOR x = 0 TO 5

20 FOR y = 0 TO 5

25 IF x*y>9 GOTO 50

30 PRINT x*y

40 NEXT y

50 NEXT x


Note the new line 25 which branches the program to the NEXT X instruction. You don't have to worry about leaving the NEXT Y loop open since SMARTBASIC and most other BASICs will automatically terminate the 'y' loop when it encounters the NEXT X instruction which is higher in the shell:


/ 10 FOR x = 0 TO 5

outer| 20 FOR y = 0 TO 5\

loop| 25 IF x*y>9 GOTO 50 |inner

| 30 PRINT x*y |loop

| 40 NEXT y/

\ 50 NEXT x


Note that when using this technique you must always specify the variable name in NEXT statements. Use this approach only with extreme caution; it is very easy to get all tangled up.

Routine Addresses:

The execution of FOR starts at 8557 (216D hex) and is very complex. It evaluates the FROM TO and STEP variables in floating point and stores all that information on the STACK. It then places the NEXT execution address on the stack and saves the stack address in register IY. If all this sounds complicated, IT IS! it is therefore not advisable to mess with any part of this code. The NEXT execution ranges from 8749 to 8954 (222D to 22FA hex) and is just as complicated.

Back to Top