foreach
Loops once for each element in an array or list.
Syntax
foreach(loop_var = matr_expr)
Input
- loop_var
- A variable.
- matr_expr
- An array or a list of elements.
Example
{num = 0}
{gmat = {10, 20, 30, 40}}
{foreach(elem = gmat); num++}
Element {num}: {elem}
{endloop}
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Comments
A foreach loop consists of a foreach statement, a list of instructions, and an endloop statement. The instructions between foreach and endloop are carried out for each element in matr_expr. When the foreach loop begins, loop_var equals the first element in matr_expr. Each time through the loop, loop_var takes on the value of the next element in matr_expr. When the last element is reached, the loop ends and the statement following the endloop statement is executed.
Every foreach loop must be terminated with an endloop statement.