alternative_20for_20next_20behaviour
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
alternative_20for_20next_20behaviour [2018/04/17 15:03] – Added syntax highlighting tbest3112 | alternative_20for_20next_20behaviour [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Alternative FOR NEXT behaviour===== | =====Alternative FOR NEXT behaviour===== | ||
- | //by Richard Russell, July 2007//\\ \\ Although the **FOR**...**NEXT** loop is fundamental to all versions of BASIC, dialects differ in its detailed implementation. In **BBC BASIC** FOR...NEXT loops have the following properties:\\ \\ | + | //by Richard Russell, July 2007//\\ \\ Although the **FOR**...**NEXT** loop is fundamental to all versions of BASIC, dialects differ in its detailed implementation. In **BBC BASIC** FOR...NEXT loops have the following properties: |
* 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: | ||
<code bb4w> | <code bb4w> | ||
FOR i% = 1 TO 0 | FOR i% = 1 TO 0 | ||
Line 16: | Line 17: | ||
PRINT i% | PRINT i% | ||
</ | </ | ||
- | The first loop prints **1**, even though the ' | + | The first loop prints **1**, even though the ' |
+ | |||
+ | The second loop prints **11**, which is one more than the ' | ||
+ | |||
+ | 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:// | ||
+ | |||
+ | Some BASIC dialects //do not execute the FOR loop at all// if the initial conditions aren't met, i.e. the ' | ||
<code bb4w> | <code bb4w> | ||
FOR loopvar = start TO finish | FOR loopvar = start TO finish | ||
Line 23: | Line 30: | ||
NEXT loopvar | NEXT loopvar | ||
</ | </ | ||
- | Some BASIC dialects exit the loop //with the loop variable equal to the ' | + | Some BASIC dialects exit the loop //with the loop variable equal to the ' |
<code bb4w> | <code bb4w> | ||
FOR loopvar = start TO finish | FOR loopvar = start TO finish | ||
Line 30: | Line 37: | ||
NEXT loopvar | NEXT loopvar | ||
</ | </ | ||
- | Of course you can combine both behaviours if you wish:\\ \\ | + | Of course you can combine both behaviours if you wish: |
<code bb4w> | <code bb4w> | ||
FOR loopvar = start TO finish | FOR loopvar = start TO finish | ||
Line 38: | Line 45: | ||
NEXT loopvar | NEXT loopvar | ||
</ | </ | ||
- | 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> | <code bb4w> | ||
FOR loopvar = start TO finish STEP -delta | FOR loopvar = start TO finish STEP -delta |
alternative_20for_20next_20behaviour.1523977405.txt.gz · Last modified: 2024/01/05 00:18 (external edit)