haxe Loops For


Example

For-loops iterate over an iterating collection. An iterating collection is any class which structurally unifies with Iterator<T> or Iterable<T> types from the Haxe standard library.

A for-loop which logs numbers in range 0 to 10 (exclusive) can be written as follows:

for (i in 0...10) {
    trace(i);
}

The variable identifier i holds the individual value of elements in the iterating collection. This behaviour is similar to for-each in other languages.

A for-loop which logs elements in an array can therefore be written as follows:

for (char in ['a', 'b', 'c', 'd']) {
    trace(char);
}

Try the example on try.haxe.org.

References