FOR (For)

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:

  1. If the conditioning indicators on the FOR statement line are satisfied, the FOR operation is processed (step 2). If the indicators are not satisfied, control passes to the next operation to be processed following the associated END or ENDFOR statement (step 8).
  2. If specified, the initial value is assigned to the index name. Otherwise, the index name retains the same value it had before the start of the loop.
  3. If specified, the limit value is evaluated and compared to the index name. If no limit value is specified, the loop repeats indefinitely until it encounters a statement that exits the loop (such as a LEAVE or GOTO) or that ends the program or procedure (such as a RETURN).

    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.

  4. The operations in the FOR group are processed.
  5. If the conditioning indicators on the END or ENDFOR statement are not satisfied, control passes to the statement after the associated END or ENDFOR and the loop ends.
  6. If the increment value is specified, it is evaluated. Otherwise, it defaults to 1.
  7. The increment value is either added to (for TO) or subtracted from (for DOWNTO) the index name. Control passes to step 3. (Note that the conditioning indicators on the FOR statement are not tested again (step 1) when control passes to step 3.)
  8. The statement after the END or ENDFOR statement is processed when the conditioning indicators on the FOR, END, or ENDFOR statements are not satisfied (step 1 or 5), or when the index value is greater than (for TO) or less than (for DOWNTO) the limit value (step 3), or when the index value overflows.
Note:
If the FOR loop is performed n times, the limit value is evaluated n+1 times and the increment value is evaluated n times. This can be important if the limit value or increment value is complex and time-consuming to evaluate, or if the limit value or increment value contains calls to subprocedures with side-effects. If multiple evaluation of the limit or increment is not desired, calculate the values in temporaries before the FOR loop and use the temporaries in the FOR loop.

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.

Figure 323. Examples of the FOR Operation
 *..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


[ Top of Page | Previous Page | Next Page | Contents | Index ]