a programming loop which is executed the specified number of times.
For i=x To | UpTo | DownTo y [Step z]
// program-segment
[condition] Exit For
Exit For If [condition]
Next [i] | EndFor [i]
i:avar; any numeric variable
x, y, z:aexp; arithmetic expression
A For...Next loop begins by initializing the loop counter i to the specified starting value. With each run the loop counter is incremented or decremented by the specified amount (in case of default by 1, otherwise by the step value in z). When the counter over- or underflows the loop criterion in y, the command after the next Next is unconditionally branched to.
In the following structure:
For i = x To y
// program segment
Next i
the loop counter i is incremented by 1 every time the loop runs through Next i. The loop ends when i overflows the loop criterion value y.
In the following structure:
For i = x To y Step z
// program segment
Next i
Every time the loop runs through Next i, the loop counter i is incremented by step amount in z, if this amount is positive, or is decremented by step amount in z, if this amount is negative.
The loop ends when i, for Sgn(z)=1, overflows the loop criterion value y or, for Sgn(z)=-1, underflows the loop criterion value y.
The following structure:
For i = x DownTo y
// program segment
Next i
is a special case of:
For i=x To y Step z, where z=-1.
The loop counter i is decremented by 1 every time the loop runs through Next i. The loop ends when i underflows the loop criterion value in y. Step can also be used with DownTo to decrement the count by values greater than 1, but it must always have a negative value.
If, at the very beginning of the loop, the loop counter i is already greater than (for For...To) or less than (for For...DownTo or For...To...Step z, when z<0) the loop criterion y, the loop is not executed.
In the following structure:
For i = x To i + y
// program segment
Next i
... the (i + y) loop criterion is calculated before the loop is started, rather then re-evaluated with every increase in i.
Only integers should be used with For...Next loops NOT decimal/floating point numbers, as with the latter the count may fail to reach the end of the loop - sometimes because the Single or Double accumulated value is actually larger the the end of loop criterion, sometimes because if a combination of variable types is used, one may not exactly match the others. For more information, see the Remarks section below.
By using an Exit For command, the For...Next loop can be terminated regardless of whether the loop condition is fulfilled.
Finally, EndFor can be used in place of Next.
Do note, that the loop criterion in the For...Next loop must always be numeric!
For loop criteria which are not numeric the While…Wend, Repeat...Until or Do...Loop loops must be used.
Local n As Int32, s As Double
// Prints 1 to 7 then exits loop
For n = 1 To 10
Print n`
Exit For If n = 7
EndFor n
// Prints 10 down to 1
For n = 10 DownTo 1
Print n`
Next n
// Print 1.1 to 1.8 then exits loop
For s = 1.1 UpTo 2.2 Step 0.1
Print s`
If s NEAR 1.8 Then Exit For
Next s
As long as different loop counter variables are used the For...Next loops can be embedded to any number of levels.
As noted above, only Integers should be used in a For...Next loop as, otherwise, the loop may not complete. This, and a workaround, are shown below:
looperror(4.2, 4)
newloop(4.2, 4)
Debug.Show
Proc newloop(vm As Double, s As Double)
// Alternative by James Gaite 28th March 2018
Debug "Alternative loop from 0 to" & vm & " through" & s & " iterations."
Local Int32 ct = Round(vm / (vm / s)), v
For v = 0 To ct : Debug (vm / s) * v : Next v
EndProc
Proc looperror(vm As Double, s As Double)
// Bug report by Code Labs 28th March 2018
Debug "BUG double: missing 4.2"
Local Double v
For v = 0.0 To CDbl(vm) Step CDbl(vm / s) : Debug v : Next v
EndProc
For Each, While Wend, Repeat Until, Do Loop, ExitFor
{Created by Sjouke Hamstra; Last updated: 28/03/2018 by James Gaite}