<!-- PDF page 23 -->

Two **FOR ... NEXT** loops must either be one inside the other, or be completely separate.

Another thing to avoid is jumping into the middle of a **FOR … NEXT** loop from the outside. The control variable is only set up properly when its **FOR** statement is executed, and if you miss this out the **NEXT** statement will confuse the computer. You will probably get an error report saying **NEXT without FOR** or **Variable not found**.

There is nothing whatever to stop you using **FOR** and **NEXT** in a direct command. For example, try:

```
FOR m=0 TO 10: PRINT m: NEXT m
```

You can sometimes use this as a (somewhat artificial) way of getting round the restriction that you cannot **GO TO** anywhere inside a command – because a command has no line number. For instance:

```
FOR m=0 TO 1 STEP 0: INPUT a: PRINT a:
NEXT m
```

The step of zero here makes the command repeat itself forever.

This sort of thing is not really recommended, because if an error crops up then you have lost the command and will have to type it in again –and **CONTINUE** will not work.

For additional speed and efficiency, *NextBASIC* also allows integer variables to be used as the index in **FOR … NEXT**, eg:

```
10 FOR %i=%$c9 TO 220
20   PRINT %i
30 NEXT %i
```

Integer loops run much faster than loops using a standard floating point control variable so they're preferred, especially where speed is a concern. Remember, however, that there is only a limited amount of integer variables, so a small bit of planning is warranted before starting with your program.

### EXIT

Sometimes we need to prematurely exit from a loop, be it a **FOR...NEXT** (See previous section) or **REPEAT...REPEAT UNTIL** (See next section) one. There is a seemingly obvious solution to that; jump out of the loop with **GO TO** however this is ill advised as **GO TO** doesn't exit a loop "cleanly" and therefore should not be used. Instead, there's a specialised command however that allows for a "clean" exit and that is (the aptly named):

**EXIT** [*n*]

where *n* is an optional line number or label to jump to. **EXIT** on its own will jump to the next statement after the end of the loop. Consider this example (the actual syntax of the loop is explained in the next section):

```
100 REPEAT
110   INPUT n
120   IF n=33 THEN EXIT 150
130 REPEAT UNTIL n<0
140 PRINT "Loop ended normally"
150 PRINT "Loop ended early"
```

The loop above will terminate normally (when the condition set on line 130 is satisfied) when you enter a negative value but early if you input enter the value **33**. **EXIT** can also be used to get out of nested loops using successive **EXIT** statements on the same line within

