Acorn Computers BBC Micro Model B w/8271 — Acorn BBC Micro Model B w/8271 User Guide Manual PDF

231 FOR Purpose The word FOR is one of the words used in a FOR...NEXT loop. This makes the computer execute a series of statements a specified number of times; for example 120 FOR X=1 TO 5 130 PRINT X 140 NEXT X would print out the numbers 1 , 2 , 3 , 4 , 5 . The variable X in the above example initially takes on the value 1 and the program then goes through until it reaches the word NEXT . The program then returns to the line or statement FOR X=1 TO 5 and X is increased in value by 1. The program continues the loop, increasing the value of X in steps of 1, until X reaches 5. After that, the program no longer loops; instead it moves onto the next statement after the FOR...NEXT loop.

As an option the ‘step size’ can be changed. In the example above X increased by 1 each time around the loop. In the next example XYZ increases by 0.3 each time around the loop. 230 FOR XYZ=5 TO 6 STEP 0.3 240 PRINT XYZ 250 NEXT XYZ The above program would print out the numbers 5 5.3 5.6 5.9 The value of XYZ on exit from the above program would be 6.2. The step size may be negative if you wish to make the value of the control variable decrease each time around the loop. 870 FOR r2d2%=99 TO 60 STEP -12 880 PRINT r2d2%; "Hi there" 890 NEXT r2d2%