Step increment
Parameters
| increment - amount added to the loop counter each pass; must be a constant value, not a variable; 1 (default) when Step is left off |
Description
|
Sets how much a For loop's counter changes each pass. Step is the optional tail of a For line. Leave it out and the counter goes up by one. Step 2 visits every other value, which is handy for drawing a chequerboard or thinning out a particle sweep, and Step -1 counts down: For i = 10 To 1 Step -1. A float counter takes a float step, so For a# = 0 To 1 Step 0.25 gives you 0, 0.25, 0.5, 0.75 and 1 - a neat way to walk a fade or a lerp. Bear in mind that floats do not land on exact values, so a step like 0.1 may stop a fraction short of the limit. The step has to be a constant. Writing Step speed where speed is a variable will not compile, because the compiler uses the sign of the step at build time to decide whether the loop counts up or down. Loops with a changing stride belong in a While or Repeat block where you do the adding yourself. Step 0 never moves the counter, so the loop either spins forever or never starts. If you want an endless loop, say so with Repeat ... Forever. Step is also a reserved word, so you cannot use it as a variable name. See also: For, To, Next, Each, Exit. |
Example
; Step Example ; ------------ ; Step sets how much the counter changes each pass - here, 2 at a time Print "Placing a fence post every 2 metres:" For metre=0 To 10 Step 2 Print " Post at "+metre+"m" Next Print "" ; A negative Step counts down Print "Countdown:" For count=5 To 1 Step -1 Print " "+count+" ..." Next Print " Lift off!" Print "" Print "Press any key to close the example" WaitKey End
Index