Anvil | Smiths Smiths | Register Register | Login Login |
Search:
Show links Show tools Show tree | Previous document Next document | njet.org > Anvil > Documentation > Language reference > Statements > Compound statements > Foreach

Foreach

Foreach statement iterates through elements of (iterable) value given by the last expression and evalutes contained statements during each iteration. Three pieces of values can be retrieved (all of which are optional) at each iteration:

  • Index, numerical index starting from zero and increasing by one on each iteration.
  • Key, key of mapping in container, if container has no notaion of mapping, then numerical index is used.
  • Value, element in container.
Target expressions must be assignable.

It is also possible to supply multiple assignment targets for index, key and value. If so then the corresponding value returned by enumeration is enumerated itself and elements yielded by this enumeration are assigned in order to the target expressions.

A break statement will terminate execution of loop, while continue skips rest of statements in body and starts next iteration cycle.

  foreach :=  
    "foreach" "(" [[[[ foreach-expr-listindices ] ";"]  
                     [ foreach-expr-listkeys    ] ";"]  
                     [ foreach-expr-listvalues  ] ";"] expriterable ")" statement 
  foreach-expr-list := assignable-exprtarget ( "," assignable-exprtarget ) * 
Contributes notes:
Add a note
What's new | Anvil