Usage

Syntax:
  1. for (begin statement; condition; loop statement) { }
  2. for (needle: array haystack) { }

Explanation

for produces a loop like while, however, it is slightly more flexible.

There are two possible syntaxes to use with for.

Using syntax 1, the loop starts by executing begin statement, and then as long as condition is true, the code in the block will be executed followed by the loop statement. This is useful for creating counter loops.

Using syntax 2, the loop runs once for each item that is in the given haystack array. The needle variable is set to the next value of the haystack array, and the next loop runs. This is used for iterating through arrays easily.

Example

Syntax 1

for (this.counter = 0; this.counter < 10; this.counter ++)
{
  echo(this.counter);
}

... will echo 0, 1, 2, 3, ... until 9.

Syntax 2

temp.array = {"foo", "bar", "baz"};

for (temp.item: temp.array)
{
  echo(temp.item);
}

... will echo "foo", "bar" and "baz".

Categories

CategoryLanguageConstruct

There are no comments on this page. [Add comment]

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki