Statements: for loop

The for loop iteration statement lets you repeat a nested statement a specified number of times.

Syntax

Form 1:

for( ;condition;loop‑expression)statement

Form 2:

for(init‑expression;condition;loop‑expression)statement

Discussion

The for loop statement executes the nested statement repeatedly as long as the condition evaluates to the boolean value true. The condition is evaluated at the begin of every iteration. In turn, with the end of an iteration, the loop-expression is evaluated. Usually, the loop-expression is used to increment or decrement a counter variable. For example:

array string text[ 16 ]; var int32 i = 0; // Initialize all 16 elements of the one-dimensional array for ( ; i < text.size; i = i + 1 ) text[ i ] = "";

In the second form of the for statement you can specify an init-expression, which is evaluated just before the first iteration. Usually, the init-expression is used to initialize a counter variable. To execute multiple statements within the loop, you can group those together within a block statement. For example:

var int32 i; // Hide all text views existing within the current GUI component for ( i = CountViews(); i > 0; i = i - 1 ) { var Core::View view = GetViewAtIndex( i - 1 ); var Views::Text textView = (Views::Text)view; // Is this a text view? if ( textView != null ) textView.Visible = false; }