- For ... To ... Next ... Step always performs one iteration of the loop no matter what is specified for the starting, ending and step values. E.g. For X=X1 To X2 : <do something> : Next X will perform one loop even if X1 is greater than X2.
I'm not sure that that is a bug. For... Next loops should always run once, because no testing of the values is done until the Next instruction is executed.
No, sorry, it
is a bug. The way you describe it is the way AMOS currently handles it - as though the test doesn't occur until the
Next is reached. But it's actually supposed to be done where the
For part is.
A
For ... Next loop is always regarded as a shorthand for a specific type of
While ... Wend loop. So the test to terminate the loop is at the beginning of the loop, not at the end. If the termination test returns False upon entry, the loop is never executed.
It's the opposite structure to a
Repeat ... Until loop where the termination test is always at the end of the loop. So a
Repeat ... Until will always execute at least one iteration of the loop.
For Counter = StartValue To EndValue Step StepValue
Statements
Next Counteris equivalent to:
Counter = StartValue
While Counter * Sgn(StepValue) <= EndValue * Sgn(StepValue)
Statements
Counter = Counter + StepValue
WendIf you try
For I = 2 To 1 : Print I : Next I in any other version of Basic, you'll see what I mean.
(Nerdy techo note. If you've never programmed in C, quit reading now before brain damage sets in. Well, that's how C syntax always grabs
me.
)
It derives from the C language standard:
for ((expr1; expr2; expr3)
statementis equivalent to:
expr1;
while (expr2) {
statement
expr3;
}See Kernighan and Richie - The C Programming Language ('The White Book'
).