FOREACH
Runs a block for each of some points, or of some values.
Syntax
FOREACH name IN [first..last] BEGIN … END
FOREACH name IN [point,point,…] BEGIN … END
FOREACH name IN value,value,… BEGIN … END
FOREACH name IN list BEGIN … END
FOREACH name IN dictionary BEGIN … END
| Argument | Meaning |
|---|---|
| name | The variable that takes each item in turn. |
| [first..last] | A run of points, such as [P0..P3]: name is each one's position. |
| [point,point,…] | Some points, such as [P0,P2]: name is each one's position. |
| value,value,… | Some expressions, each worked out when the loop starts: numbers, positions, strings. |
| list | A list, such as {10,20,40} or a variable that holds one: name is each item. |
| dictionary | A dictionary: name is each key (dictionary[name] its value). |
DO may go before BEGIN.
Examples
FOREACH Q IN [P0..P3] BEGIN
R = Q + [20, 0]
CIRCLE R, Q, TRUE
END

A circle of radius 20 at each corner of the box, that follows the corners when they move.
FOREACH S IN 10, 20, 40 BEGIN
P10 = [L / 2, W / 2]
P11 = P10 + [S, 0]
CIRCLE P11, P10, TRUE
END
Three circles round the middle of the box, of radius 10, 20 and 40.
HOLES={"small":5,"large":12}
FOREACH K IN HOLES BEGIN
P10 = [HOLES[K] * 10, 50]
P11 = P10 + [HOLES[K], 0]
CIRCLE P11, P10, TRUE
END
A hole for each entry of a dictionary, its radius the entry's value.
Notes
A position is used where an element wants a point by its name: CIRCLE R, Q, TRUE above is a circle through R
centred on Q.
BREAK leaves the loop; CONTINUE goes on to the next item.
See also: Blocks and control flow, FOR.