Previous: , Up: Loops   [Contents][Index]

18.10.3 for-in

The for-in statement has this form:

for (formal in container [where exp]) stmt

where, in each iteration, the name formal will be associated with consecutive values of container, which shall be an expression evaluating to an array or a string. formal is available in stmt, which is the statement executed in each iteration.

If the where part is specified, then only iteration in which exp holds true are processed. formal can be referred in exp. Note that this doesn’t mean the loop will stop after processing the first “not selected” element. See the following example:

(poke) for (c in [1,2,3,4] where c % 2) printf " %v", c
 1 3

It is possible to leave the loop from within stmt using the break statement.

It is also possible to jump to the next iteration of the loop from within stmt using the continue statement.