Home
Products
Community
Manuals
Contact
Login or Signup

Blitz3D Docs -> For

For variable

Parameters:

variable = any valid variable name

Description:

The first command of the FOR ... NEXT loop, this command is used to assign a variable to a range of numbers in sequence and execute a set of code that many times. Using the STEP command allows you to skip a certain value between each loop of the code. Note that the STEP amount cannot be a variable.

This is frequently used when a specific pattern of numbers is needed to perform an evolution (moving something from point A to point B, adding a value to a score incrementally, etc). This allows you to assign a variable with the current value of the loop. See the example for more.
Note: Unlike many BASIC languages, the NEXT command does NOT use the FOR command's variable as an identifier. If you have nested FOR ... NEXT commands, the language will automatically match the NEXT with the nearest FOR command.

See also: To, Step, Each, Next, Exit, While, Repeat.

Example:

; Print the values 1 through 10
For t = 1 To 10
Print t
Next

; Print the values 1,3,5,7,9
For t = 1 To 10 Step 2
Print t
Next

Comments

David Boudreau(Posted 1+ years ago)
Also, note that a typical use of the For/Next loop in graphics programming is to use a nested structure where you have an X value nested inside a Y value, such as;

For y = 0 to 15
For x = 0 to 15
Plot x,y
Next; matches x
Next; matches y

The reason for putting Y on the outside is because it will process the Ploting in a rasterization method, i.e. start at top left corner, go all the way across to the right top side, then the next line down, etc. until the last line on the bottom.


Zethrax(Posted 1+ years ago)
Something to be aware of is that the expression used in the For command's 'to' parameter is evaluated for each loop iteration, which may be undesirable.

Demo:-
For i = FromParamFunc() To ToParamFunc()
 Print i
Next

WaitKey

End

;---

Function FromParamFunc()
	Print "from"
	Return 1
End Function

Function ToParamFunc()
	Print "to"
	Return 5
End Function


If you are using a complex expression for the 'to' parameter, you may be better off if you store the result of the expression in a variable and use the variable as the parameter instead.


Yo! Wazzup?(Posted 1+ years ago)
Thanks for the help Bill =]


Blitz3D Manual Forum

BlitzPlus Equivalent Command