Welcome to the eight part of the ST User BASIC Tutorial series. 
                     
     Following on from string handling last month, Peter Phillips 
                     
     looks at the GOTO statement and FOR... NEXT loops.
                     
                     
                     
     FIRST STEPS IN BASIC
                     
                     
                     
     The ST is well known as a fast computer. The 8Mhz 68000 
                     
     processor at the heart of the machine can manage about 1MIPS, or 
                     
     one million instructions per second. As a measure of computing 
                     
     speed, this is very impressive for a home computer. Up until 
                     
     now, we've been looking at how BASIC can be used to perform 
                     
     simple arithmetic and string operations. Most of these can be 
                     
     done with a pocket calculator, and often considerably faster. 
                     
     These "drainpipe" calculations are so called because they are 
                     
     simple and the program falls straight through, starting at the 
                     
     first line of code and working its way down to the bottom. This 
                     
     sort of calculation is quite common, but more often than not a 
                     
     calculation will have to be performed over and over again while 
                     
     a program is running. In an example, say that the program had to 
                     
     obtain a single name several times. If each request for a name 
                     
     was written on a separate line, then the program will grow at a 
                     
     quite alarming rate. Fortunately, BASIC has a simple answer to 
                     
     repetitive tasks, called LOOPS.
                     
                     
                     
     LOOP THE LOOP
                     
                     
                     
     There are two types of loops available to BASIC, the simple GOTO 
                     
     statement and the more complex FOR... NEXT loop.
                     
                     
                     
     GOT KNOTTED
                     
                     
                     
     The GOTO statement, by its nature, should only be used with 
                     
     extreme care. It is very easy to construct a program with so 
                     
     many GOTO statements in it that it becomes almost impossible to 
                     
     keep track of what the program is doing.
                     
                     
                     
                     
                     
     PROGRAM TWO
                     
                     
                     
     10 GOTO 50
                     
     20 GOTO 40
                     
     30 PRINT "ARRIVED AT LAST!"
                     
     40 GOTO 30
                     
     50 GOTO 20
                     
                     
                     
     When this program is run, it rather effectively ties itself up 
                     
     in knots. It still works, but what happens if it doesn't? 
                     
     Imagine a program that was 15 or 20K long that had this sort of 
                     
     GOTO structure in it. If the program didn't work (and it 
                     
     wouldn't; one of the GOTO statements would almost certainly 
                     
     point to the wrong place) it would be impossible to debug, as 
                     
     the structure of the program has become so confused.
                     
                     
                     
     CONTROL YOURSELF
                     
                     
                     
     The FOR... NEXT loop is the most commonly used loop in BASIC and 
                     
     it provides a much greater degree of control over the way a 
                     
     program executes than a GOTO statement. FOR... NEXT is a 
                     
     controlled loop, and its most common is as a closed loop.
                     
                     
                     
     PROGRAM THREE
                     
                     
                     
     10 FOR n=1 TO 10
                     
     20 PRINT "Number = ";n
                     
     30 NEXT
                     
     40 PRINT "Final = ";n
                     
                     
                     
     As you can see, the result of this program is more complex than 
                     
     GOTO. The program counts from 1 to 10 and then prints the final 
                     
     result. In effect, a simple counter has been implemented. The 
                     
     program is split into three distinct parts:
                     
                     
                     
     1> Line 10 is the start of the loop and sets up the basic 
                     
     structure. The FOR... TO part tells BASIC to start counting at 1 
                     
     and finish counting at 10. The loop counter has been called 'n' 
                     
     although it could be any letter we like. There is an optional 
                     
     command that can be used in the loop called STEP, which tells 
                     
     BASIC how big the steps to count in should be. For example, if 
                     
     line 10 read:
                     
                     
                     
     10 FOR n=5 to 50 STEP 5
                     
                     
                     
     BASIC would count from 5 to 50 in steps of 5, so it would go 5, 
                     
     10, 15, 20... etc. If the STEP command is omitted, then BASIC 
                     
     will default to a step size of 1.
                     
                     
                     
     2> Line 20 forms the main body of the loop. It prints out the 
                     
     current value of the counter 'n'. Any BASIC commands can be used 
                     
     it this part of the loop, and it can be as many lines long as 
                     
     you like. One word of warning, DO NOT USE A GOTO COMMAND TO JUMP 
                     
     OUT OF A CONTROLLED LOOP! BASIC will get totally confused and 
                     
     strange things may start to happen. Never, NEVER jump out of a 
                     
     controlled loop!
                     
                     
                     
     3> Line 30 is the tail of the loop. It instructs BASIC to 
                     
     increment the counter by the STEP value and perform any 
                     
     instructions between the lines that contains the FOR and
                     
     NEXT instructions. The NEXT command is conditional, and before 
                     
     the counter is incremented, it is checked against the limits set 
                     
     in the FOR statement. If those limits are exceeded then the loop 
                     
     terminates.
                     
                     
                     
     4> Line 40 is outside the structure of the loop and simply 
                     
     prints the final value of 'n'. This is always one step 
                     
     higher than the number of loop counts, or iterations.
                     
                     
                     
     As an interesting experiment, try making the loop count 
                     
     backwards. Simply replace line 10 with:
                     
                     
                     
     10 FOR n=10 TO 1
                     
                     
                     
     The program suddenly stops working! Why? If you look back 
                     
     through the previous explanation of how the FOR... NEXT loop 
                     
     works, you'll find the answer.
                     
                     
                     
     STEP LIVELY...
                     
                     
                     
     In the previous program, we were simply counting forwards, from 
                     
     1 to 10. BASIC assumes the STEP size to be one, but it always 
                     
     assume a PLUS one step size. When counting backwards from 10 to 
                     
     1 with a step size on +1, the first time NEXT is called it 
                     
     attempts to increase n by +1 and the conditions of the loop are 
                     
     exceeded. To get BASIC to count backwards, we must instruct it 
                     
     to use a loop that has a negative step size:
                     
                     
                     
     10 FOR n= 10 to 1 STEP -1
                     
                     
                     
     In a similar manner, it is possible to get BASIC to count 
                     
     negative numbers. Try replacing line 10 with:
                     
                     
                     
     10 FOR n=10 TO -10 STEP -2
                     
                     
                     
     Notice that the step size has also be increased to -2, so only 
                     
     eleven (10,8,6,4,2,0,-2,-4,-6,-8,-10) iterations are needed to 
                     
     complete the loop.
                     
                     
                     
     One of the most useful features about FOR... NEXT loops is the 
                     
     way they can be nested, one inside another. Nesting is a process 
                     
     where one controlled loop is actually with in the body of 
                     
     another loop. There are practical limits to how deep loops can 
                     
     be nested, but no program should ever approach the limit. If it 
                     
     does, then the program needs to be re-written to make it 
                     
     simpler.
                     
                     
                     
     PROGRAM FOUR
                     
                     
                     
     10 FOR a=1 TO 10
                     
     20 FOR b=1 TO 10
                     
     30 PRINT a*b
                     
     40 NEXT
                     
     50 NEXT
                     
                     
                     
     The body of the first loop is controlled by the counter 'a' and 
                     
     is executed 10 times. The body of the second loop is controlled 
                     
     by 'b', but for some reason it is executed 100 times. I'll leave 
                     
     you to ponder on why 'b' is executed 100 times, with the answer 
                     
     in next months column. The NEXT part of the loop can optionally 
                     
     be followed by the variable it refers to. So lines 40 and 50 can 
                     
     be changed to:
                     
                     
                     
     40 NEXT b
                     
     50 NEXT a
                     
                     
                     
     Or should that be the other way around...?
                     
                     
                     
                     
                     
     BEFORE NEXT TIME...
                     
                     
                     
     1. Write a program using a FOR.. NEXT loop to print out the 8 
                     
     times table, with the results formatted like this:
                     
                     
                     
     1 * 8 = 8
                     
     2 * 8 = 16... etc.
                     
                     
                     
     2. Try using the GOTO statement to find out exactly how many 
                     
     FOR... NEXT loops can be nested. This will create an error 
                     
     message from BASIC. NOTE: how deep loops can be nested depends 
                     
     on which type of BASIC you're using.
                     
                     
                     
     3. Try mixing up the FOR... NEXT loops in Program Four by 
                     
     changing the NEXT statements by placing the variable name after 
                     
     the NEXT statement.
                     
                     
                     
                     

Takaisin

(C) Marko, Suomen Atari-sivut / ArkiSTo 2003