User Tools

Site Tools


simulating_20continue

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
simulating_20continue [2018/04/17 19:48]
tbest3112 Added syntax highlighting
simulating_20continue [2022/04/23 10:38] (current)
richardrussell Added alternative methods
Line 1: Line 1:
 =====Simulating CONTINUE===== =====Simulating CONTINUE=====
  
-//by Richard Russell, November 2012//\\ \\  Some dialects of BASIC (e.g. Visual Basic) have a **CONTINUE** 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:\\ \\ +//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: 
 + 
 <code bb4w> <code bb4w>
         WHILE some_condition         WHILE some_condition
Line 11: Line 16:
         ENDWHILE         ENDWHILE
 </​code>​ </​code>​
 +
 +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:​
 +
 +<code bb4w>
 +        WHILE some_condition
 +          REM Do some things
 +          IF next_iteration THEN ENDWHILE WHILE FALSE : REM CONTINUE
 +          REM Do some more things
 +        ENDWHILE
 +</​code>​
 +
 +The **ENDWHILE WHILE FALSE** compound statement behaves to all intents and purposes exactly like CONTINUE. ​ The equivalent for a REPEAT loop would be:
 +
 +<code bb4w>
 +        REPEAT
 +          REM Do some things
 +          IF next_iteration THEN UNTIL FALSE REPEAT : REM CONTINUE
 +          REM Do some more things
 +        UNTIL some condition
 +</​code>​
 +
 +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:
 +
 +<code bb4w>
 +        REPEAT
 +          REM Do some things
 +          IF next_iteration THEN UNTIL FALSE REPEAT EXIT REPEAT : REM CONTINUE
 +          REM Do some more things
 +        UNTIL some condition
 +</​code>​
 +
simulating_20continue.txt · Last modified: 2022/04/23 10:38 by richardrussell