This particular form of the FOR statement has been introduced in the latest versions of Delphi and in FPC.
It is implemented in PMP as "standard Pascal", even if some behaviors differ a bit.
for-iterator = FOR { EACH } variable IN enumerable DO block .
<enumerable> may be:
<variable>:
Any simple variable compatible with the <enumerable> element type.
Unlike a classic FOR loop which may loop with index values, the iterator loops on all element values contained in <enumerable>: this element value will be written in <variable>.
Both <variable> and <enumerable> are read-only during the loop.
The iterator behavior will differ a bit according to the type of <enumerable>:
For an ARRAY <variable> will contain all values contained in the array, the loop is implemented as an equivalent of:
<temp> := LOW(<enumerable>);
WHILE TRUE DO
BEGIN
<variable> := <enumerable>[<temp>];
<block>
IF <temp> < HIGH(<enumerable>) THEN
INC(<temp>)
ELSE
BREAK;
END;
For a STRING <variable> will contain all characters of the string; if the string is empty, <block> will not be executed. The loop is implemented as an equivalent of:
<temp> := 0;
WHILE TRUE DO
IF <temp> = length(<enumerable>) THEN
BREAK
ELSE
BEGIN
INC(<temp>);
<variable> := <enumerable>[<temp>];
<block>
END;
For an enumerated TYPE <variable> will contain all possible values of the type. The loop is implemented as an equivalent of:
FOR <variable> := LOW(<enumerable>) TO HIGH(<enumerable>) DO
<block>
For a simple type variable <variable> will contain all bit positions that are 1. The loop is implemented as an equivalent of:
FOR <temp> := 0 TO (SizeOf(<enumerable>) * 8) - 1 DO
IF <temp> IN <enumerable> THEN
<block>
The loop flow may also be controlled via BREAK and CONTINUE statements.
See also: BREAK, CONTINUE, FOR statement.