iterative loop

An '''iterative loop''' repeatedly executes a set of instructions as the [iterator] steps through a series of values. Types of iterative loops include [for] loops and [foreach] loops.

For loops

A '''for loop''' is an '''iterative loop''' that repeatedly runs a [block] of instructions against an [iterator] value. The [block] containing the [print] statement in the following example runs for each consecutive [loop]. The [iterator] variable l, starts at zero and [increment]s on each consecutive [loop]:

BEGIN { for (l= 0; l <= 9; l++) { print l } }

_Elements of a for loop_

The [parentheses] contains three elements:

_Initialization statement_

The initialization statement executes before the [loop] starts. In the above example, l = 0 is the initialization statement.

_Test condition_

The test condition, is evaluated before each iteration of the [loop], and if it evaluates to [true], the [loop] is executed. If the test condition does not evaluate to [true], then the [loop] is terminated. In the above example the l <= 9 is used as a test condition. This causes the loop to exit, when the [value] of l reaches 10:

for (l= 0; l <= 9; l++) { print l }

_Continuation statement_

The continuation statement is executed on completion of each iteration of the loop. The continuation statement, l++ in the above example causes the value of l to [increment] on each iteration of the [loop].

Foreach loops