EXIT

This is an experimental feature that may change in future versions.
It is provided for compatibility with the Windows version of BBC BASIC and to enhance the code readibility of the Z80 version.

EXIT FOR causes execution to continue after the matching NEXT statement, EXIT REPEAT causes execution to continue after the matching UNTIL statement and EXIT WHILE causes execution to continue after the matching ENDWHILE statement. Typically you would use EXIT when a situation occurs which necessitates leaving the loop 'part way through':

FOR I% = start% TO finish%
  ...
  IF bool% THEN EXIT FOR
  ...
NEXT I%
REPEAT
  ...
  IF bool% THEN EXIT REPEAT
  ...
UNTIL condition%
WHILE condition%
  ...
  IF bool% THEN EXIT WHILE
  ...
ENDWHILE

You can EXIT from a loop even when inside a nested loop of a different kind:

REPEAT
  FOR J% = first% TO last%
    ...
    IF bool% THEN EXIT REPEAT
    ...
  NEXT
UNTIL FALSE
REM Execution continues here

EXIT is not compatible with the use of non-standard FORNEXT constructs such as 'popping' an inner loop or listing multiple loop variables after NEXT. When EXIT is used, every FOR must have a matching NEXT. The following is permitted:

FOR I%=1 TO 3
  FOR J%=4 TO 5
    IF bool% THEN EXIT FOR I%
  NEXT J%
NEXT I%

However, the following are not.

FOR I%=1 TO 3
  FOR J%=4 TO 5
    IF bool% THEN EXIT FOR I%
  NEXT J%,I%
FOR I%=1 TO 3
  FOR J%=4 TO 5
    IF bool% THEN EXIT FOR I%
  NEXT I%

Syntax

Associated Keywords