for - coldrockgames/doc-scriptor GitHub Wiki
The for
loop, is an Entry-controlled loop. It's exit condition is evaluated at the beginning of each iteration. If the condition is false before the first iteration, the loop body will not execute.
for <variable> = <begin> to <end> [step <increment>]
// ...
next
The syntax of the for loop in scriptor is the same as in the BASIC language. You declare a variable and specify the range it should iterate over. Optionally you may add a step size (the increment) of each iteration in the loop. This increment can also be a negative number to create count-down loops instead of count-up loops.
By default, the increment is set to 1
.
The code inside the loop is repeated until the value of the variable is exact or greater (or less than, if counting down) the end value. If the variable matches the end value exactly, the loop will execute one final time.
In other words, in a for i = 1 to 10
, the first loop iteration starts with i = 1
and the final iteration runs with i = 10
.
Both the starting and ending points are inclusive.
A simple count-up loop:
for i = 1 to 10
// ...
next
All three parts of a for
loop may be expressions, so this is valid:
for i = self.bullets.current to self.bullets.maximum step self.bullets.ammosize
// ...
next
Counting down to zero:
for i = 10 to 0 step -1
// ...
next
The next statement closes the loop-body and returns to the beginning of the loop, where the exit condition is evaluated.