Bungee Logic : Statement : Collection Iteration

Page Status: Beta

Back to Bungee Logic Statements

Contents

[edit] Description

A loop that executes (at most) once for each element of a collection.

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 each element in collection { }

Where:

element Is the element of the current iteration step.
collection Is the collection that will be iterated.

[edit] Logic

Collection iteration loops look like this:

for each CurrentElement in COLLECTION
{
  var Data CurrentElement;
  var int Length;
  var boolean StopIterating;
  var int CurrentIndex;
 

     // Your code goes here


}

The statement block executes repeatedly, with CurrentIndex going from 0 up to COLLECTION.numItems-1. For each iteration, CurrentElement is set to the CurrentIndex-th item in COLLECTION. CurrentIndex is checked against the current value of COLLECTION.numItems before every iteration, so it is the collection's current numItems that controls loop termination, not the value it had when the loop started. The intrinsic variable Length, on the other hand, is set when the loop begins, and its value does not change even if the size of the collection changes.

The loop can be terminated prematurely by executing a return statement, by an uncaught error generated in the loop body, or by setting StopIterating to true. Errors and return statements cause immediate loop termination, but setting StopIterating to true does not. 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 (though you can set a breakpoint), nor does it have a continue statement.

The COLLECTION may be any site (a Var, Path, Expression, etc) of a Collection type. It is evaluated only once, when the loop begins. For example, if COLLECTION is a variable referring to a certain collection, that collection will be the subject of the loop even if the variable is set to null (or to a different collection) while the loop is running.

[edit] Error Handling

  • The type for this var is automatically assigned according to the element type in the collection being iterated. In some cases, you will need to change the automatically assigned type for this var in order to work with the members of the element type.
  • Changing the name of CurrentElement will break its functionality in the collection iteration.

[edit] Intrinsics

Variables

  • StopIterating — Setting this to true will terminate the iteration after the current loop finishes.
  • Length — Contains number of elements in the collection.
  • CurrentIndex — Contains the current value of the counter. This variable may be read, but not written, by user code.
  • CurrentElement — References the element at the current index of the iteration. Note: The type can be safely changed if needed.

[edit] Properties

  • Collection — A site that designates which collection to use for iteration.
  • Reverse Iterate — Specifies whether the iteration proceeds in reverse order (from end to beginning).
  • Maximum — The maximum number of iterations that may occur before the loop terminates automatically.

Tab Title Bar

[edit] See Also

Return

Expressions Cheat Sheet


Log in if you would like to rate this page.
    Copyright © 2005 - 2007 Bungee Labs. All rights reserved.