Simulating CONTINUE
by Richard Russell, November 2012, extended April 2022
Many languages, including some dialects of BASIC (e.g. Visual Basic), have a CONTINUE or CONT statement, which continues execution at the next iteration of a FOR, REPEAT or WHILE loop. It is equivalent to jumping to the NEXT, UNTIL or ENDWHILE statement (i.e. it skips the remainder of the 'body' of the loop).
The same effect may be achieved in BBC BASIC by using a GOTO, but obviously that isn't very satisfactory. A better way is to use a dummy REPEAT…UNTIL TRUE loop as follows:
WHILE some_condition REPEAT REM Do some things IF next_iteration THEN EXIT REPEAT : REM CONTINUE REM Do some more things UNTIL TRUE ENDWHILE
But this still isn't ideal: the indentation is confusing and EXIT REPEAT now exits from the dummy loop rather than from the outer loop (if that happens to be a REPEAT loop). Fortunately there is an alternative which avoids these shortcomings:
WHILE some_condition REM Do some things IF next_iteration THEN ENDWHILE WHILE FALSE : REM CONTINUE REM Do some more things ENDWHILE
The ENDWHILE WHILE FALSE compound statement behaves to all intents and purposes exactly like CONTINUE. The equivalent for a REPEAT loop would be:
REPEAT REM Do some things IF next_iteration THEN UNTIL FALSE REPEAT : REM CONTINUE REM Do some more things UNTIL some condition
Unlike the WHILE case, however, this does have a flaw: it will not do the right thing if there's an EXIT REPEAT statement in the first part of the loop. This can be resolved as follows:
REPEAT REM Do some things IF next_iteration THEN UNTIL FALSE REPEAT EXIT REPEAT : REM CONTINUE REM Do some more things UNTIL some condition