For : Next
DescriptionFor <variable> = <expression1> To <expression2> [Step <constant>] ... Next [<variable>]
For : Next is used to create a loop within a program with the given parameters. At each loop the <variable> value is increased by a 1, (or of the "Step value" if a Step value is specified) and when the <variable> value is above the <expression2> value, the loop stop.
With the Break command its possible to exit the For : Next loop at any moment, with the Continue command the end of the current iteration can be skipped.
The For : Next loop works only with integer values, at the expressions as well at the Step constant. The Step constant can also be negative.
For k = 0 To 10 ... NextIn this example, the program will loop 11, time (0 to 10), then quit.
For k = 10 To 1 Step -1 Debug k NextIn this example, the program will loop 10 times (10 to 1 backwards), then quit.
a = 2 b = 3 For k = a+2 To b+7 Step 2 ... Next kHere, the program will loop 4 times before quitting, (k is increased by a value of 2 at each loop, so the k value is: 4-6-8-10). The "k" after the "Next" indicates that "Next" is ending the "For k" loop. If another variable, is used the compiler will generate an error. It can be useful to nest several "For/Next" loops.
For x=0 To 320 For y=0 To 200 Plot(x,y) Next y Next xNote: Be aware, that in PureBasic the value of <expression2> ('To' value) can also be changed inside the For : Next loop. This can lead to endless loops when wrongly used.