The Beginner's Page
The computer executes BASIC program lines in numerical order unless the program tells the computer to do otherwise. (An exception is Amiga BASIC, where lines are executed in physical order unless the program instructs otherwise.) There are several ways to transfer control to a different line. This month we'll discuss one of those ways: looping.
GOTO tells the computer to go to a another line specified by a line number (or to a line label in Amiga BASIC and Atari BASIC). Some examples are:
150 GOTO 390 (transfer to line 390) 550 GOTO WHEELS (transfer to line labeled WHEELS)
You can transfer to a previous line, a later line, or even the same line. A loop is created when the computer executes a statement or several statements repeatedly. If you are in the middle of a GOTO loop, you can stop the computer by a pressing a break key or key combination—quite often Control-C; press RUN/STOP on Commodore computers.
Here is an example of a program using GOTO statements to alter the normal flow of a program:
20 PRINT "ONE" 30 GOTO 60 40 PRINT "TWO" 50 GOTO 80 60 PRINT "THREE" 70 GOTO 40 80 PRINT "FOUR" 90 GOTO 90 100 END
Instead of printing the words ONE, TWO, THREE, FOUR in order as they appear in the program, the computer moves around. First the word ONE is printed; then the computer is instructed to GOTO line 60, which prints THREE. Next the computer executes GOTO 40, which branches back to line 40 where TWO is printed. Then the computer finds GOTO 80, which prints the word FOUR. Line 90 says to GOTO 90, which means the computer will repeatedly execute line 90—it will be stuck in a loop. You don't actually see anything happening on the screen, but the program will not end, so you know you are in a loop and need to break out of the program to regain control of the computer. Sometimes this type of loop is used to keep something (such as graphics) on the screen.
Another type of GOTO loop could be like this:
20 PRINT "HELLO" 30 GOTO 20
And here is a GOTO loop using a variable:
20 A = A + 1 30 PRINT A 40 GOTO 20
The FOR and NEXT statements create a loop that is executed a specified number of times before the program continues. Here's an example:
20 FOR C = 1 To 5 30 PRINT C 40 NEXT C
In the example above, the variable C (use any variable name you wish) is a counter or index. Line 20 says to start the counter C at 1 and proceed until C is equal to 5. Line 30 prints the value of C. Line 40 designates the end of the loop with NEXT C, which increments C by one. The computer checks to see whether C has exceeded the limit of 5. If not, the program transfers to the statement directly following the FOR statement. This process continues until the limit is exceeded; then the program continues with the line after NEXT. When C is printed in this example, C will be 1, 2, 3, 4, and 5. When C is 6, the loop finishes, and the program segment ends.
The counter variable does not have to be used within the loop; it can be used simply to run the loop a certain number of times, as in the following:
20 FOR T = 1 TO 8 30 PRINT "HI" 40 NEXT T
The increments do not have to be by one. You can specify a STEP size. Suppose you want to count by twos:
20 FOR N = 0 TO 10 STEP 2 30 PRINT N 40 NEXT N
The STEP size can even be a fraction:
20 FOR X = 1 TO 4 STEP .5 30 PRINT X 40 NEXT X
The STEP size may be negative, which means the counter (or index) would be decreased each time:
20 FOR J = 10 TO 0 STEP -1 30 PRINT J 40 NEXT J
Any of the numbers in the FOR statement may be variables. For example, if you have previously defined or calculated A, B, and S, you may use
20 FOR N = A TO B STEP S . . . 50 NEXT N
There can be any number of statements between the FOR and NEXT statements, but there must be a NEXT statement to correspond to each FOR statement. You can even create nested loops, or loops within loops. When working with nested loops, you must make sure your second loop is totally contained within the first loop as the following lines show:
20 FOR A = 1 TO 3 30 FOR B = 1 TO 5 40 PRINT A;" * "; B;" = "; A * B = 50 NEXT B
60 PRINT 70 NEXT A 80 END
If there are no statements between a FOR statement and a NEXT statement, the computer simply counts, which creates a delay during program execution. An example is:
20 PRINT "ONE" 30 FOR DELAY = 1 TO 800 40 NEXT DELAY 50 PRINT "TWO" 60 END
WHILE-WEND loops (not available in all BASICs) are similar to FOR-NEXT loops except that, as a programmer, you do not need to specify limits or calculate a number of times the loop needs to perform. The computer performs the loop WHILE a certain condition is met. The loop is ended with WEND which can be thought of as WHILE-END. Here's an example:
20 WHILE SCORE < 10 …(programming for a game) 780 WEND 790 …(programming for end of game)
Here is another example of a WHILE-WEND loop. The computer performs the loop WHILE the name entered is not REGENA. Therefore, when the name REGENA is entered, the loop will end.
20 WHILE NAME$ <> "REGENA" 30 PRINT "ENTER A NAME." 40 INPUT NAME$ 50 WEND 60 PRINT "LOOP ENDS." 70 END