For : Next


Syntax
For <variable> = <expression1> To <expression2> [Step <constant>]
  ...
Next [<variable>]
Description
The For : Next function is used to cause a loop within a program within given parameters. At each loop the <variable> is increased by a factor of 1, (or of the "Step value" if a Step value is specified), when the <variable> value equals the <expression2> 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 skiped.

Example:

  For k = 0 To 10 
    ...
  Next
In this example, the program will loop 11, time (0 to 10), then quit.

Example:

  a = 2
  b = 3 
  For k = a+2 To b+7 Step 2
    ...
  Next k  
Here, 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 is useful when nesting some "For/Next" expressions.

Example:

  For x=0 To 320 
    For y=0 To 200 
      Plot(x,y)
    Next y
  Next x
Note: 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.