User Tools

Site Tools


alternative_20for_20next_20behaviour

Differences

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

Link to this comparison view

alternative_20for_20next_20behaviour [2018/03/31 13:19]
127.0.0.1 external edit
alternative_20for_20next_20behaviour [2018/04/17 15:03] (current)
tbest3112 Added syntax highlighting
Line 5: Line 5:
   * The body of the loop is always executed at least once, irrespective of the values specified in the FOR statement.   * The body of the loop is always executed at least once, irrespective of the values specified in the FOR statement.
   * On exit from the loop the loop variable has a value //greater than// (for a positive STEP) the TO value specified in the FOR statement, i.e. one more STEP has taken place.   * On exit from the loop the loop variable has a value //greater than// (for a positive STEP) the TO value specified in the FOR statement, i.e. one more STEP has taken place.
-\\  The following examples illustrate this behaviour:​\\ \\ +\\  The following examples illustrate this behaviour:​\\ \\ 
 +<code bb4w>
         FOR i% = 1 TO 0         FOR i% = 1 TO 0
           PRINT i%           PRINT i%
         NEXT i%         NEXT i%
 +</​code>​ 
 +<code bb4w>
         FOR i% = 1 TO 10         FOR i% = 1 TO 10
         NEXT i%         NEXT i%
         PRINT i%         PRINT i%
 +</​code>​
 The first loop prints **1**, even though the '​finish'​ value is less than the '​start'​ value.\\ ​ The second loop prints **11**, which is one more than the '​finish'​ value.\\ \\  On occasion you may wish to emulate the behaviour of a different BASIC dialect, for example when porting a program to BBC BASIC. One convenient way of doing that is to use the [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin5.html#​exit|EXIT FOR]] statement available in //BBC BASIC for Windows// version 5.60a or later.\\ \\  Some BASIC dialects //do not execute the FOR loop at all// if the initial conditions aren't met, i.e. the '​finish'​ value is less than the '​start'​ value (with a positive STEP). To emulate that behaviour use code similar to the following:​\\ \\  The first loop prints **1**, even though the '​finish'​ value is less than the '​start'​ value.\\ ​ The second loop prints **11**, which is one more than the '​finish'​ value.\\ \\  On occasion you may wish to emulate the behaviour of a different BASIC dialect, for example when porting a program to BBC BASIC. One convenient way of doing that is to use the [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin5.html#​exit|EXIT FOR]] statement available in //BBC BASIC for Windows// version 5.60a or later.\\ \\  Some BASIC dialects //do not execute the FOR loop at all// if the initial conditions aren't met, i.e. the '​finish'​ value is less than the '​start'​ value (with a positive STEP). To emulate that behaviour use code similar to the following:​\\ \\ 
 +<code bb4w>
         FOR loopvar = start TO finish         FOR loopvar = start TO finish
           IF loopvar > finish THEN EXIT FOR           IF loopvar > finish THEN EXIT FOR
           REM Body of loop here           REM Body of loop here
         NEXT loopvar         NEXT loopvar
 +</​code>​
 Some BASIC dialects exit the loop //with the loop variable equal to the '​finish'​ value//. To emulate that behaviour use code similar to the following:​\\ \\  Some BASIC dialects exit the loop //with the loop variable equal to the '​finish'​ value//. To emulate that behaviour use code similar to the following:​\\ \\ 
 +<code bb4w>
         FOR loopvar = start TO finish         FOR loopvar = start TO finish
           REM Body of loop here           REM Body of loop here
           IF loopvar >= finish THEN EXIT FOR           IF loopvar >= finish THEN EXIT FOR
         NEXT loopvar         NEXT loopvar
 +</​code>​
 Of course you can combine both behaviours if you wish:\\ \\  Of course you can combine both behaviours if you wish:\\ \\ 
 +<code bb4w>
         FOR loopvar = start TO finish         FOR loopvar = start TO finish
           IF loopvar > finish THEN EXIT FOR           IF loopvar > finish THEN EXIT FOR
Line 29: Line 37:
           IF loopvar >= finish THEN EXIT FOR           IF loopvar >= finish THEN EXIT FOR
         NEXT loopvar         NEXT loopvar
 +</​code>​
 In all cases if the STEP is negative you need to reverse the comparisons:​\\ \\  In all cases if the STEP is negative you need to reverse the comparisons:​\\ \\ 
 +<code bb4w>
         FOR loopvar = start TO finish STEP -delta         FOR loopvar = start TO finish STEP -delta
           IF loopvar < finish THEN EXIT FOR           IF loopvar < finish THEN EXIT FOR
Line 35: Line 45:
           IF loopvar <= finish THEN EXIT FOR           IF loopvar <= finish THEN EXIT FOR
         NEXT loopvar         NEXT loopvar
 +</​code>​
alternative_20for_20next_20behaviour.txt · Last modified: 2018/04/17 15:03 by tbest3112