| Construct: | For..To..Step..Next |
| Type: | Looping construct |
| Usage: |
|
For 1 -> A To 2
A - 1 -> A
Next
Changing the value of <step> from within the loop causes similar results.
Note also that unlike many other programming languages, in a simple For..Next loop, when the calc exits the loop the value of <var> is between <end> (inclusive) and <end> + <step> (exclusive).
The basic problems which have been found so far with For..Next are detailed below.
For 1 -> A To 2
Break
For 1 -> B To 2
"FOO"
Next
"BAR"
Next
This will result in the output "BAR", followed by a syntax error caused by the second Next, which it considers to be unmatched, having jumped to after the first Next, rather than the second. This also affects the other looping constructs, but as it is most common to use nested Fors I have mentioned this here.
For 1 -> A To 10
A = 4 => Goto 0
Next
Lbl 0
This is because when the calculator gets to the Lbl 0 it still thinks that it is in the loop, so it expects to find a Next. This is not strictly speaking a bug, as it makes logical sense and may even be desirable.
In a simple loop such as the one shown above, this is not a problem, as you can replace the Goto 0 with a Break. Problems arise if:
For 1 -> A To 10
A = 4 => 99 -> A
Next
A = 99 => Goto 0
...
...
Lbl 0
Here 99 is not just being used as a flag, but also to exit the loop. When the calc reaches the Next, it realises that A+1 is more than 10 and exits. Because of this, you would need to jump to just before the Next using an If or a Goto if you had other statements between the 99 -> A and the Next.
In the second case, a similar but slightly more complicated method should be used:
For 1 -> A To 10
If A = 4
Then 99 -> A
Else For 1 -> B To 2
"FOO"
Next
"BAR"
IfEnd
Next
This demonstrates the use of the jumping to before the Next using an If as mentioned above.
The problems described here are caused by a bug in the calculator which affects all of the looping constructs. It is currently explained under Break, but I may move it to its own section at some point.
Written in notepad and compiled with WTA, for clean, consistent HTML.