Bungee Logic : Statement : For Iteration
Back to Bungee Logic Statements
Contents |
[edit] Description
A loop that executes its body at most COUNT times. An Intrinsic Variable named CurrentIndex will have the values 0, 1, 2, ..., COUNT-1 on successive iterations (or, for a reverse iteration, the values COUNT-1, ..., 2, 1, 0). CurrentIndex is of type int32. If COUNT is less than 1, the loop body doesn't execute at all.
Warning All Bungee Iteration constructs (for, while, and collection) share certain behaviors that are important to understand to avoid subtle bugs. In particular, each iteration construct has a set of Intrinsic Variables that are provided for programming convenience and to give control over the loop. These Intrinsic Variables must not be deleted or renamed, and should only be modified as documented. Also, variable initializers declared in the loop body are run only once, rather than once per iteration as you might have expected. For the gory details, see Iteration Basics.
[edit] Syntax
for ( initial_expression; test_expression; increment_expression ) { }
Where:
initial_expression Sets the initial value of the loop counter. test_expression Is a site designating the maximum number of iterations for the loop increment_expression Increments the loop counter.
[edit] Logic
For loops may run with increasing or decreasing values of CurrentIndex, depending on the setting of Reverse Iterate. An ascending loop looks like this:
for (CurrentIndex = 0; CurrentIndex < COUNT; CurrentIndex++)
{
var int CurrentIndex;
var boolean StopIterating;
// Your code goes here
}
The statements in the block execute at most COUNT times, with CurrentIndex taking on successive int32 values from 0 up to COUNT-1. The loop can be terminated early by setting StopIterating to true, by executing a return statement, or by an error being propagated out of the loop. Setting StopIterating to true does not cause the loop to exit immediately. Instead, as the next iteration begins, StopIterating is tested; if it is true, then the loop will terminate.
Bungee Logic does not provide a break statement, nor does it have a continue statement.
The COUNT value may be any site (a Var, Path, Expression, etc), but it is evaluated only once. After the loop begins execution, changes to the count expression will not affect the loop. If you need that kind of flexibility, use a while instead.
If you turn on Reverse Iterate, you get a descending iterator:
for (CurrentIndex = COUNT-1; CurrentIndex >= 0; CurrentIndex--)
{
var int CurrentIndex;
var boolean StopIterating;
// Your code goes here
}
CurrentIndex will take on successive int32 values from COUNT-1 down to 0. Otherwise, a backward iteration is just like a forward iteration.
[edit] Error Handling
-None-
[edit] Intrinsics
Variables
- StopIterating — Setting this to true will terminate the iteration after the current loop finishes.
- CurrentIndex — Contains the current value of the counter. This variable may be read, but not written, by user code.
[edit] Properties
- Count — A site that designates the number of times to repeat the loop.
- Reverse Iterate — Specifies that the counter is decremented rather than incremented.
- Maximum — The maximum number of iterations that may occur before the loop terminates automatically.
Tab Title Bar
- Comment Out — Comments out the entire section of code.
- Set Breakpoint — Sets a breakpopint for debugging purposes.