Free-Form Syntax | FOR{(MR)} index-name {= start-value} {BY increment} {TO|DOWNTO limit} |
Code | Factor 1 | Extended Factor 2 | ||||
---|---|---|---|---|---|---|
FOR | index-name = start-value BY increment TO | DOWNTO limit |
The FOR operation begins a group of operations and controls the number of times the group will be processed. To indicate the number of times the group of operations is to be processed, specify an index name, a starting value, an increment value, and a limit value. The optional starting, increment, and limit values can be a free-form expressions. An associated END or ENDFOR statement marks the end of the group. For further information on FOR groups, see Structured Programming Operations.
The syntax of the FOR operation is as follows:
FOR index-name { = starting-value }
{ BY increment-value }
{ TO | DOWNTO limit-value }
{ loop body }
ENDFOR | END
The starting-value, increment-value, and limit-value can be numeric values or expressions with zero decimal positions. The increment value, if specified, cannot be zero.
The BY and TO (or DOWNTO) clauses can be specified in either order. Both "BY 2 TO 10" and "TO 10 BY 2" are allowed.
In addition to the FOR operation itself, the conditioning indicators on the FOR and ENDFOR (or END) statements control the FOR group. The conditioning indicators on the FOR statement control whether or not the FOR operation begins. These indicators are checked only once, at the beginning of the for loop. The conditioning indicators on the associated END or ENDFOR statement control whether or not the FOR group is repeated another time. These indicators are checked at the end of each loop.
The FOR operation is performed as follows:
If the TO clause is specified and the index name value is greater than the limit value, control passes to the first statement following the ENDFOR statement. If DOWNTO is specified and the index name is less than the limit value, control passes to the first statement after the ENDFOR.
Remember the following when specifying the FOR operation:
See LEAVE (Leave a Do/For Group) and ITER (Iterate) for information on how those operations affect a FOR operation.
For more information, see Structured Programming Operations.
*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
/free
// Example 1
// Compute n!
factorial = 1;
for i = 1 to n;
factorial = factorial * i;
endfor;
// Example 2
// Search for the last nonblank character in a field.
// If the field is all blanks, "i" will be zero.
// Otherwise, "i" will be the position of nonblank.
for i = %len (field) downto 1;
if %subst(field: i: 1) <> ' ';
leave;
endif;
endfor;
// Example 3
// Extract all blank-delimited words from a sentence.
WordCnt = 0;
for i = 1 by WordIncr to %len (Sentence);
// Is there a blank?
if %subst(Sentence: i: 1) = ' ';
WordIncr = 1;
iter;
endif;
// We've found a word - determine its length:
for j = i+1 to %len(Sentence);
if %subst (Sentence: j: 1) = ' ';
leave;
endif;
endfor;
// Store the word:
WordIncr = j - i;
WordCnt = WordCnt + 1;
Word (WordCnt) = %subst (Sentence: i: WordIncr);
endfor;
/end-free