# For

The For statement works in conjunction with the Next statement to execute a loop. FOR begins the loop and specifies how many times it is to be executed.

In the following example, the FOR X=1 in line 55 sets X to a value of 1. The TO 7 specifies that the loop should end when X exceeds a value of 7, and STEP 2 indicates that the value of X should increment by two after each trip through the loop:

```00055 FOR X=1 TO 7 STEP 2
00065 PRINT X
00075 NEXT X
00085 PRINT X; "DONE"
```
Movement through the above loop proceeds as follows
1.) Line 55 sets X to 1 and tests to see if X is greater than 7;
2.) Line 65 prints the 1; line 75 calculates the next X and performs a loop test to see if the ending value (greater than 7) has been reached. Since STEP 2 is indicated, the next value of X is 3. The process in lines 65-75 is repeated until the next increment of X is greater than 7;
3.) Control then passes to statement 85, that will print a message that the loop is done and that the value of X is now 9:

You may nest FOR/NEXT loops. In the following example, the internal loop is executed 1000 times each time the external loop is executed:

```00615 FOR A=12 TO 34
o
o
o
00665 FOR N=1 TO 1000
o
o
o
00705 NEXT N
o
o
o
00735 NEXT A
```

All FOR statements must have a corresponding NEXT statement. In nested loops, the internal FOR and NEXT statements must be contained entirely within the external loop.

### Syntax

```FOR <numeric variable> = <numeric expression> TO <numeric expression> [STEP <numeric expression>]
```

1. Step by 1.

### Parameters

The FOR statement has three required parameters. The "numeric variable" initially takes the value of the first "numeric expression". When it exceeds the value of the second "numeric expression", the loop is terminated.

The optional "STEP numeric expression" parameter specifies the amount by which the num-var should increment after each loop. It may be either a negative or a positive value.

### Technical Considerations

1.) When the value of the STEP num-expr is zero, the loop continues indefinitely -unless a statement within the loop causes the num-var to exceed the specified final value. Since STEP allows only addition or subtraction, you could use this feature to increment the num-var through multiplication or division, as in the following example:
```00010 FOR X=1 TO 10 STEP 0
00020 X=X*2
00030 NEXT X
```
2.) While every FOR statement must have a corresponding NEXT statement, you may pass control into or out of a loop without generating errors.
3.) The allowable number of loop variables for FOR/NEXT loops is determined by the FORSTACK specification in the BRConfig.sys file; its default is 20. The number of occurrences for each loop variable is not limited.
4.) The NEXT statement both calculates the next value and performs a loop test. When the loop test fails, it is because the value of the num-var has exceeded the specified ending value. The num-var retains this value even though the loop is terminated. In the following example, then, line 7740 prints 12 as the value of R, even though the ending value of the loop is just 10:
```07720 FOR R=2 TO 10 STEP 2
07730 NEXT R
07740 PRINT R
```
5.) Since the FOR statement also tests the loop variable at the beginning of a loop, a FOR/NEXT loop may not always be executed, as in the following example. The FOR statement would pass control to the first statement after the loop in this instance:
```05100 FOR I=9 TO 5
05110 PRINT I
05120 NEXT I
```
6.) FOR uses the current RD setting as its basis for numeric comparisons. See RD (in BRConfig.sys) for additional information.