PERFORM Statement

The PERFORM statement executes a procedure with optional loop control.

Format 1

PERFORM [ IN THREAD ]

      [ procedure-1 [ {THROUGH} procedure-2 ] ]
                      {THRU   }

      [ HANDLE IN handle-1 ]

      [ statement END-PERFORM ]

Format 2

PERFORM [ IN THREAD ]

      [ procedure-1 [ {THROUGH} procedure-2 ] ]
                      {THRU   }

      [ HANDLE IN handle-1 ]

        number TIMES
      [ statement END-PERFORM ]

Format 3

PERFORM [ IN THREAD ]

      [ procedure-1 [ {THROUGH} procedure-2 ] ]
                      {THRU   }

      [ HANDLE IN handle-1 ]

      [ WITH TEST {BEFORE} ] UNTIL condition
                  {AFTER }

      [ statement END-PERFORM ]

Format 4

PERFORM [ IN THREAD ]

      [ procedure-1 [ {THROUGH} procedure-2 ] ]
                      {THRU   }

      [ HANDLE IN handle-1 ]

      [ WITH TEST {BEFORE} ]
                  {AFTER }

        VARYING counter FROM starting-val

            BY increment UNTIL condition

      [ AFTER counter FROM starting-val

            BY increment UNTIL condition ] ...

      [ statement END-PERFORM ]

Syntax Rules

  1. handle-1 is a HANDLE or HANDLE OF THREAD data item.
  2. procedure-1 and procedure-2 are paragraph or section names in the Procedure Division.
  3. statement is an imperative statement.
  4. number is an integer numeric literal or data item. The value of number cannot exceed 2,147,483,647.
  5. condition is a conditional expression.
  6. counter is a numeric data item.
  7. starting-val is a numeric literal or data item.
  8. increment is a non-zero numeric literal or data item.
  9. A PERFORM statement must have exactly one of the procedure-1 or the statement END-PERFORM phrases specified or a period (".") for an implied END-PERFORM.
  10. The words THRU and THROUGH are interchangeable.

General Rules

  1. A PERFORM statement that contains procedure-1 is an out-of-line PERFORM. When statement is used instead, then it is an in-line PERFORM.
  2. An in-line PERFORM statement functions according to the same rules for an otherwise identical out-of-line PERFORM except that statement is executed in place of the statements in the range of procedure-1 (through procedure-2 if specified).
  3. When the PERFORM executes, control transfers to the first statement of procedure-1. Control might not transfer, however, depending on the evaluation of condition (if specified). The PERFORM statement also establishes an implicit transfer of control to the end of the PERFORM statement according to the following rules:
    1. If procedure-1 is a paragraph name, and procedure-2 is not specified, the return is after the last statement of procedure-1
    2. If procedure-1 is a section name, and procedure-2 is not specified, the return is after the last statement of the last paragraph of procedure-1
    3. If procedure-2 is specified and is a paragraph name, the return is placed after the last statement of procedure-2.
    4. If procedure-2 is specified and is a section name, the return is placed after the last statement of the last paragraph of procedure-2.
    5. If an in-line PERFORM is specified, an execution of the PERFORM statement is completed after statement has executed.
  4. procedure-1 and procedure-2 are not necessarily related except that control starts at procedure-1 and returns when it reaches the end of procedure-2. In particular, GO TO and PERFORM statements may occur between procedure-1 and the end of procedure-2.
  5. Control can pass to statements that are inside the range of procedure-1 through procedure-2 by mechanisms other than PERFORM. In this case, the implicit return to the PERFORM referencing these statements is not made. An implicit return occurs only for an active PERFORM.
  6. The range of a PERFORM statement consists of those statements that are executed as a result of executing that PERFORM. This includes statements that are executed as the result of GO TO and PERFORM statements included in the range of the PERFORM statement.
  7. If that range of a PERFORM statement includes another PERFORM statement, the range of the included PERFORM must be either totally included in or totally excluded from the logical sequence of the first PERFORM statement. Thus an active PERFORM included in the range of another active PERFORM may not allow control to pass to the return point of the first PERFORM. Furthermore, two or more active PERFORM statements may not have a common return point.
  8. Within a thread, a paragraph under the control of a PERFORM statement may (directly or indirectly) PERFORM itself only if the compile-time option -Zr1 is specified (this option is specified by default).
  9. If the TEST phrase is not specified, TEST BEFORE is implied.
  10. When the THREAD option is used, a new thread is created by the PERFORM statement. Once control returns to the end of the PERFORM statement, the thread is terminated. Note that all of the statements contained in the scope of the PERFORM are executed in the new thread. This includes any loop control operations implied by the PERFORM. For example, the statement:
      PERFORM THREAD, PARA-1 5 TIMES

    creates a single thread the performs PARA-1 five times (as opposed to creating five separate threads, each of which executes PARA-1 once).

  11. If handle-1 is specified, the new thread's unique ID is stored in handle-1.

Format 1 General Rules

A Format 1 PERFORM statement executes its range exactly once.

Format 2 General Rules

A Format 2 PERFORM statement executes its range a fixed number of times. If number is zero or negative, control passes to the end of the PERFORM statement. Otherwise the range of the PERFORM statement executes number times. Changing the value of number during the execution of the PERFORM statement does not change the number of times that range is executed.

Format 3 General Rules

A Format 3 PERFORM statement executes its range until condition evaluates true. If TEST BEFORE is specified or implied, the evaluation of condition occurs prior to any executions of the PERFORM range. Thus if condition is true when the PERFORM starts, the range will not be executed. If TEST AFTER is specified, the evaluation of condition does not occur until after the first execution of the PERFORM range.

Format 4 General Rules

  1. A Format 4 PERFORM statement executes its range a variable number of times while systematically changing the value of one or more variables.
  2. If TEST BEFORE is specified or implied and only one counter is specified:
    1. counter is set to the value of starting-val when the PERFORM statement begins.
    2. If condition is false, the PERFORM range executes once. Then increment is added to counter and condition is evaluated again. This cycle repeats until condition is true.
    3. If condition is true when the PERFORM statement begins executing, control is passed to the end of the statement after counter is set to starting-val..
  3. If TEST BEFORE is specified or implied and two or more counters are used:
    1. Each counter is set to the value of the corresponding starting-val.
    2. If the first condition is true, control transfers to the end of the PERFORM statement.
    3. If the last condition is false, the range of the PERFORM executes once. The final counter is incremented by the corresponding increment and the last condition is evaluated again. This cycle continues until the last condition is true.
    4. When the last condition is true, the last counter is set again to the corresponding starting-val.. The preceding counter is then incremented by the corresponding increment and the preceding condition is evaluated. If the condition is false, step (c) is performed again.
    5. When the condition in step (d) is true, the cycle repeats for the next higher-level counter. These cycles continue repeating in this hierarchical manner until the topmost (VARYING) counter is cycled. For each level, all levels underneath it are reinitialized and run through a full cycle each time the corresponding counter is incremented.
    6. The PERFORM statement ends when the uppermost (the first) condition evaluates true.
  4. At the end of a PERFORM with the TEST BEFORE phrase, the value of the first counter exceeds the last-used value by one addition of increment. The values of all other counters are equal to their corresponding starting-val..
  5. If the TEST AFTER phrase is specified and only one counter is used:
    1. counter is set to the value of starting-val.
    2. The range executes once. Then condition is evaluated. If it is false, increment is added to counter and the range executes again. This cycle continues until condition is true.
  6. If the TEST AFTER phrase is specified and two or more counters are used:
    1. Each counter is set to its corresponding starting-val.
    2. The PERFORM range executes once. The last condition is then evaluated. If it is false, the last counter is incremented by its corresponding increment and the PERFORM range executes again. This continues until the last condition evaluates true.
    3. When the last condition is true, the preceding condition is evaluated. If it is false, the value of the corresponding counter is incremented by its increment, the last counter is set to its corresponding starting-val, and step (b) is performed through another cycle.
    4. When the condition in step (c) is true, the cycle repeats for the next higher-level counter. These cycles continue repeating in this hierarchical manner until the topmost (VARYING) counter is cycled. For each level, all levels underneath it are reinitialized and run through a full cycle each time the corresponding counter is incremented.
    5. The PERFORM statement ends when the topmost (VARYING) condition is true.
  7. At the end of a PERFORM statement with the TEST AFTER phrase, the value of each counter is the same as at the end of the most recent execution of the PERFORM range.