1. PERFORM

The PERFORM statement explicitly transfers control to one or more procedures while the program is running. After the procedure is completed, control is passed to the next statement after the PERFORM statement.

The PERFORM statement can be used in one of the following two ways.

  • out-of-line PERFORM statement

    An out-of-line PERFORM statement does not describe a statement in the PERFORM statement, but calls the described procedure. The procedure name must be specified.

  • in-line PERFORM statement

    An in-line PERFORM statement does not specify the procedure-name, but describes a statement in the PERFORM statement. The END-PERFORM phrase must be specified to mark the end of the statement.

There are four types of PERFORM statements as follows:

Basic PERFORM Statement

The basic PERFORM statement executes the called procedures once. When the procedures called by PERFORM are completed, statements after the PERFORM statement are processed. A PERFORM statement cannot invoke itself to be executed.

figure pd perform format1
Basic PERFORM Statement Format

The following items are specified in the statement.

  • procedure-name-1, procedure-name-2

    • Both procedure-name-1 and procedure-name-2 must be the procedure names used in the procedure division.

    • When both procedure-name1 and procedure-name-2 are used, if one of them is a procedure defined in a declarative procedure, the other must also be defined in the same declarative procedure.

    • If only procedure-name-1 is used, imperative-statement-1 and END-PERFORM cannot be specified.

    • If procedure-name-1 is omitted, imperative-statement-1 and END-PERFORM must be specified.

  • imperative-statement-1

    • Statement to be executed by the PERFORM statement.

When an out-of-line PERFORM statement is executed, control is passed to the first statement of procedure-name-1. When procedure-name-1 completes, control is transferred to the next statement after the PERFORM statement.

After the procedure is performed, control is returned from the following points.

  • If procedure-name-1 is a paragraph, and procedure-name-2 is not specified, control is returned to the PERFORM statement after executing the last statement of procedure-name-1.

  • If procedure-name-1 is a section, and procedure-name-2 is not specified, control is returned to the PERFORM statement after executing the last statement of the final paragraph in the procedure-name-1 section.

  • If procedure-name-2 is a paragraph, control is returned after executing the last statement of the procedure-name-2 paragraph.

  • If procedure-name-2 is a section, control is returned after executing the last statement of the final paragraph in the procedure-name-2 section.

If both procedure-name-1 and procedure-name-2 are specified, the PERFORM statement is executed from procedure-name-1 to procedure-name-2. procedure-name-1 must precede procedure-name-2 in the program order.

A PERFORM statement can be specified in the procedure executed by a PERFORM statement.

PERFORM Statement with TIMES Phrase

In a PERFORM statement with the TIMES phrase, the procedures are called or imperative-statements are executed as many times as the number specified by the value of identifier or integer in the TIMES phrase.

After the PERFORM statement is completed, control is passed to the next statement after the PERFORM statement. If procedure-name-1 is specified, imperative-statement-1 and END-PERFORM cannot be specified.

figure pd perform format2
PERFORM Statement Format with TIMES Phrase

The following items are specified in the statement.

  • identifier-1

    • Specifies an integer item.

    • If identifier-1 is negative or zero, rather than positive, the PERFORM statement is not executed, and the statement following the PERFORM statement is performed.

    • After the PERFORM statement has been initiated, changes to identifier-1 do not affect the number of times the procedures are invoked.

  • integer-1

    • Must be a positive integer.

PERFORM Statement with UNTIL Phrase

The PERFORM statement with UNTIL phrase executes the procedures or imperative statements until the UNTIL phrase is true.

If the condition specified by the UNTIL phrase is true, control is transferred to the next statement following the PERFORM statement. If procedure-name-1 is specified, imperative-statement-1 and END-PERFORM must not be specified.

figure pd perform format3
PERFORM Statement Format with UNTIL Phrase
figure pd perform format3 phrase1
PERFORM Statement Format with UNTIL Phrase - phrase1

The following items are specified in the statement.

  • condition-1

    • Can be any condition used in OFCOBOL.

    • If the condition of the statement is true, the PERFORM statement is not executed.

If the TEST BEFORE phrase is specified, the condition is checked before executing the statement. If the TEST AFTER phrase is specified, the statement is executed at least once before the conditional statement is performed. If neither TEST BEFORE nor TEST AFTER is specified, it is assumed that the TEST BEFORE phrase is specified. If the condition is true, the next executable statement that comes after the PERFORM statement is executed.

PERFORM Statement with VARYING Phrases

The PERFORM statement with VARYING phrases increases or decreases the corresponding variable with one or more variables, and executes the procedures or imperative statements until the condition specified by the variable is true.

If procedure-name-1 is specified, imperative-statement-1 and END-PERFORM must not be specified. If procedure-name-1 is omitted, the AFTER phrase must not be specified.

figure pd perform format4
PERFORM Statement Format with VARYING Phrases
figure pd perform format4 phrase1
PERFORM Statement Format with VARYING Phrases - phrase1
figure pd perform format4 phrase2
PERFORM Statement Format with VARYING Phrases - phrase2
figure pd perform format4 phrase3
PERFORM Statement Format with VARYING Phrases - phrase3

The following items are specified in the statement.

  • identifier-2 through identifier-7

    • Must be a numeric data item.

    • Can be a floating-point data item.

  • literal-1 through literal-4

    • Must be a numeric literal.

    • Can be a floating-point data item.

  • condition-1, condition-2

    • Can be any condition used in OFCOBOL.

    • If the condition of the statement is true, the PERFORM statement is not executed.

Varying Identifiers

Whether or not the TEST BEFORE phrase is specified, each corresponding condition must be checked before executing the statement. If the TEST AFTER phrase is specified, the statement must be executed at least once before performing the conditional statement. If neither TEST BEFORE nor TEST AFTER is specified, it is assumed that the TEST BEFORE phrase is specified.

  • When varying one identifier,

    • If identifier-2 or identifier-5 is subscripted, the value is calculated whenever the data item is set or modified.

    • If identifier-3, identifier-4, identifier-6, or identifier-7 is subscripted, the value is calculated whenever each data value is set or modified.

    • The following figure shows the logic of the PERFORM statement with TEST BEFORE phrase.

      figure pd perform varying1
      Logic of the PERFORM Statement with TEST BEFORE Phrase
    • The following figure shows the logic of the PERFORM statement with TEST AFTER phrase.

      figure pd perform varying2
      Logic of the PERFORM Statement with TEST AFTER Phrase
  • When varying two identifiers,

    • In the following example, OFCOBOL checks the PERFORM statement in the following sequence.

      image

      1. Initializes IDENTIFIER-2 and IDENTIFIER-5 to IDENTIFIER-3 and IDENTIFIER-6 respectively.

      2. Processes the following according to the result of condition-1 evaluation.

        • If condition-1 is true, the PERFORM statement is terminated, and the next statement after the PERFORM statement is executed.

        • If condition-1 is false, steps 3 through 7 are executed.

      3. Processes the following according to the result of condition-2 evaluation.

        • If condition-2 is true, IDENTIFIER-4 is added to IDENTIFIER-2 and IDENTIFIER-5 is set to a current value of IDENTIFIER-6, and then step 2 is repeated.

        • If condition-2 is false, steps 4 through 6 are executed.

      4. Executes the procedures from procedure-name-1 to procedure-name-2.

      5. Increases identifier-5 by identifier-7.

      6. Repeats steps 3 through 5 until condition-2 is true.

      7. Repeats steps 2 through 6 until condition-1 is true.

        When the PERFORM statement completes, IDENTIFIER-5 has the current value of IDENTIFIER-6. IDENTIFIER-2 has a value that is greater than the last-used setting by the increment or decrement value.

    • The following figure is the logic of a PERFORM statement that contains TEST BEFORE phrase.

      figure pd perform varying3
      Logic of the PERFORM Statement with TEST BEFORE Phrase
    • The following figure shows the logic of a PERFORM statement that contains a VARYING phrase with TEST AFTER phrase.

      figure pd perform varying4
      Logic of the PERFORM Statement with TEST AFTER Phrase
  • When varying three identifiers,

    • A PERFORM statement that uses three identifiers is processed in the same way as a statement with two identifiers. It differs only in that when identifier-5 increases by identifier-7, identifier-10 is added to identifier-8, and condition-3 is checked.

      image

    • When the PERFORM statement completes, IDENTIFIER-5 and IDENTIFIER-8 has the current value of IDENTIFIER-6 and IDENTIFIER-9 respectively. IDENTIFIER-2 has a value that is greater than the last-used setting by the increment or decrement value.

    • For varying more than three identifiers, AFTER phrase(s) can be added to the previous example.

The following rules apply to the VARYING phrase in the PERFORM statement regardless of the number of identifiers that are specified.

  • When index-name is specified in the VARYING or AFTER phrases.

    • The index-name is initialized, and increased or decreased according to the index-name rules of OFCOBOL.

    • In the FROM phrase, an identifier or a literal must be a positive integer.

    • In the BY phrase, an identifier must be an integer, and a literal must be a nonzero integer.

  • When index-name is specified in the FROM phrase.

    • In the VARYING or AFTER phrase, an identifier must be an integer. The identifier is initialized internally by the SET statement.

    • In the BY phrase, an identifier must be a positive integer, and a literal must be a nonzero integer.

  • In the BY phrase, an identifier and a literal must be nonzero values.

  • In the VARYING, FROM, or BY phrase, changing the value of an identifier or index-name modifies the number of times that the procedures are executed in the PERFORM statement.