For : Next


Syntax
For <variable> = <expression1> To <expression2> [Step <constant>]
  ...
Next [<variable>]
Description
The For : Next function 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 skiped.

The For : Next function works only with integer values, at the expressions as well at the Step constant.

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.