Previous Topic Next topic Print topic


The PERFORM Statement

The PERFORM statement is used to transfer control explicitly to one or more procedures and to return control implicitly whenever execution of the specified procedure is complete.

ANS85 The PERFORM statement is used to control execution of one or more imperative statements which are in the scope of that PERFORM statement.

NETJVM The PERFORM statement is used to process each element of an array.

See also the section The EXIT Statement.

General Formats for Format 1


*

General Formats for Format 2


*

General Formats for Format 3


*

General Formats for Format 4


*
NETJVM 

General Formats for Format 5

Directives

  1. In addition to Compiler directives which provide flagging and modify the reserved word list, the following directive may impact either the syntax or the semantics described in this section.
    • PERFORM-TYPE - controls the handling of overlapping PERFORM ranges.

Syntax Rules for All Formats

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

    MF When imperative-statement-1 is specified, the END-PERFORM phrase is optional.

    COB370 Procedure-name-1 and imperative-statement-1 may both be omitted.

  2. The words THROUGH and THRU are equivalent.
  3. Where procedure-name-1 and procedure-name-2 are both specified and either is the name of a procedure in the declaratives portion of the Procedure Division, both must be procedure-names in the same declarative section.

Syntax Rules for Formats 2 and 4

  1. Each identifier represents a numeric elementary item described in the Data Division. In Format 2, identifier-1 must be described as a numeric integer.
  2. OSVSVSC2MF Floating-point data items and literals can be used anywhere a numeric data item or literal can be specified. They cannot be used where an integer is required.

Syntax Rules for Format 3

  1. If the TEST phrase is specified, the EXIT phrase must not be specified.

Syntax Rules for Formats 3 and 4

  1. ANS85 If neither the TEST BEFORE nor the TEST AFTER phrase is specified, the TEST BEFORE phrase is assumed.
  2. Condition-1, condition-2, ... , can be any conditional expression. (See the section Conditional Expressions in the chapter Procedure Division)

Syntax Rules for Format 4

  1. ANS85 In Format 4, if procedure-name-1 is omitted, the AFTER phrase must not be specified.

    ISO2002MF This restriction does not apply.

  2. Each literal represents a numeric literal.
  3. If an index-name is specified in the VARYING or AFTER phrase, then:
    1. The identifier in the associated FROM and BY phrases must reference an integer data item.
    2. The literal in the associated FROM phrase must be a positive integer.
    3. The literal in the associated BY phrase must be a nonzero integer.
  4. If an index-name is specified in the FROM phrase, then:
    1. The identifier in the associated VARYING or AFTER phrase must reference an integer data item.
    2. The identifier in the associated BY phrase must reference an integer data item.
    3. The literal in the associated BY phrase must be an integer.
  5. The literal in the BY phrase must not be zero.
  6. Two AFTER phrases are permitted in Format 4 of the PERFORM statement.

    ANS85 Six AFTER phrases are permitted in Format 4 of the PERFORM statement.

    MF This limit is raised to fifteen.

NETJVM 

Syntax Rules for Format 5

  1. NETJVM Identifier-9 must be an instance of a collection class, i.e. a class that has a GetEnumerator method returning a type that:
    1. contains a public instance method called MoveNext with no parameters;
    2. contains a public instance get property called Current. The type of this property is known as the element type of identifier-9.
    Any class that implements the System.Collections.IEnumerable interface satisfies the above rules and is therefore a collection class. All .NET arrays are such collection classes, as are the various Dictionary and List classes defined in the .NET framework.
  2. NETJVM It must be possible to perform a casting operation from the element type of identifier-9 to the type of identifier-8.

General Rules for All Formats

  1. When procedure-name-1 is specified, the PERFORM statement is referred to as an out-of-line PERFORM statement;

    ANS85  when procedure-name-1 is omitted, the PERFORM statement is referred to as an in-line PERFORM statement.

  2. The statements contained within the range of procedure-name-1 (through procedure-name-2 if specified) for an out-of-line PERFORM statement

    ANS85 or contained in the PERFORM statement itself for an in-line PERFORM statement

    are referred to as the specified set of statements.

  3. ANS85 The END-PERFORM phrase delimits the scope of the in-line PERFORM statement. (See the topic Explicit and Implicit Scope Terminators in the chapter Concepts of the COBOL Language.)
  4. ANS85 An in-line PERFORM statement functions according to the following general rules for an otherwise identical out-of-line PERFORM statement, with the exception that the statements contained within the in-line PERFORM statement are executed in place of the statements contained within the range of procedure-name-1 (through procedure-name-2 if specified). Unless specifically qualified by the word in-line or out-of-line, all the general rules which apply to the out-of-line PERFORM statement also apply to the in-line PERFORM statement.
  5. When the PERFORM statement is executed, control is transferred to the first statement of the specified set of statements (except as indicated in General Rules 8b, 8c, 8d and 8e). This transfer of control occurs only once for each execution of a PERFORM statement. For those cases where a transfer of control to the specified set of statements does take place, an implicit transfer of control to the end of the PERFORM statement is established as follows:
    1. If procedure-name-1 is a paragraph-name and procedure-name-2 is not specified, the return is after the last statement of procedure-name-1.
    2. If procedure-name-1 is a section-name and procedure-name-2 is not specified, the return is after the last statement of the last paragraph in procedure-name-1.
    3. If procedure-name-2 is specified and it is a paragraph-name, the return is after the last statement of the paragraph.
    4. If procedure-name-2 is specified and it is a section-name, the return is after the last statement of the last paragraph in the topic.
    5. ANS85 If an in-line PERFORM statement is specified, an execution of the PERFORM statement is completed after the last statement contained within it has been executed.
    6. MF  An EXIT PERFORM statement can be used to exit an in-line perform before executing all statements within the in-line perform.
  6. There is no necessary relationship between procedure-name-1 and procedure-name-2 except that a consecutive sequence of operations is to be executed beginning at the procedure named procedure-name-1 and ending with the execution of the procedure named procedure-name-2. In particular, GO and PERFORM statements can occur between procedure-name-1 and the end of procedure-name-2. If there are two or more logical paths to the return point, then procedure-name-2 can be the name of a paragraph consisting of the EXIT statement, to which all of these paths must lead.
  7. If control passes to the specified set of statements by means other than a PERFORM statement, control passes through the last statement of the set to the next executable statement as if no PERFORM statement referenced the set.
  8. The PERFORM statements operate as follows:
    1. Format 1 is the basic PERFORM statement. The specified set of statements referenced by this type of PERFORM statement is executed once and then control passes to the end of the PERFORM statement.
    2. Format 2 is the PERFORM ... TIMES. The specified set of statements is performed the number of times specified by integer-1 or by the initial value of the data item referenced by identifier-1 for that execution. If at the time of the execution of a PERFORM statement, the value of the data item referenced by identifier-1 is equal to zero or is negative, control passes to the end of the PERFORM statement. Following the execution of the specified set of statements the specified number of times, control is transferred to the end of the PERFORM statement.

      During execution of the PERFORM statement, reference to identifier-1 cannot alter the number of times the specified set of statements is to be executed from that which was indicated by the initial value of the data item referenced by identifier-1.

      Integer-1 must be zero or positive, but it may be signed.

    3. Format 3 is the PERFORM ... UNTIL. The specified set of statements is performed until the condition specified by the UNTIL phrase is true. When the condition is true, control is transferred to the end of the PERFORM statement. If the condition is true when the PERFORM statement is entered,

      ANS85 and the TEST BEFORE phrase is specified or implied,

      no transfer to procedure-name-1 takes place, and control is passed to the end of the PERFORM statement.

      ANS85 If the TEST AFTER phrase is specified, the PERFORM statement functions as if the TEST BEFORE phrase were specified except that the condition is tested after the specified set of statements has been executed. Any subscripting or reference modification associated with the operands specified in condition-1 is evaluated each time the condition is tested.

      MF When UNTIL EXIT is specified, the specified set of statements is performed repeatedly until some statement in the set causes this repetition to end. In the case of an out-of-line PERFORM, the only statements that will cause this termination are statements such as EXIT PROGRAM and STOP RUN. In the case of an in-line PERFORM, the repetition can also be terminated by the EXIT PERFORM statement and the GO TO statement.

    4. Format 4 is the PERFORM ... VARYING. This variation of the PERFORM statement is used to augment the values referenced by one or more identifiers or index-names in an orderly fashion during the execution of a PERFORM statement. In the following discussion, every reference to identifier as the object of the VARYING, AFTER, and FROM (current value) phrases also refers to index-names.

      If index-name-1 is specified, the value of identifier-3, index-name- 2, or literal-1 at the beginning of the execution of the PERFORM statement must correspond to an occurrence number of an element in the table associated with index-name-1. If index-name-3 is specified, the value of identifier-6, index-name-4, or literal-3 at the beginning of the execution of the PERFORM statement must correspond to an occurrence number of an element in the table associated with index-name-3.

      Subsequent augmentation, as described below, of index-name-1 or index-name-3 must not result in the associated index being set to a value outside the range of the table associated with index-name-1 or index-name-3; except that, at the completion of the PERFORM statement, the index associated with index-name-1 can contain a value that is outside the range of the associated table by one increment or decrement value.

      If identifier-2 or identifier-5 is subscripted, the subscripts are evaluated each time the content of the data item referenced by the identifier is set or augmented. If identifier-3, identifier-4, identifier-6, or identifier-7 is subscripted, the subscripts are evaluated each time the content of the data item referenced by the identifier is used in a setting or augmenting operation. Any subscripting or reference modification associated with the operands specified in condition-1 or condition-2 is evaluated each time the condition is tested.

      Representations of the actions of several types of Format 4 PERFORM statements are given on the following pages.

      1. If the TEST BEFORE phrase is specified or implied:

        When the data item referenced by one identifier is varied, the content of the data item referenced by identifier-2 is set to literal-1 or the current value of the data item referenced by identifier-3 at the point of initial execution of the PERFORM statement; then, if the condition of the UNTIL phrase is false, the specified set of statements is executed once. The value of the data item referenced by identifier-2 is augmented by the specified increment or decrement value (literal-2 or the value of the data item referenced by identifier-4) and condition-1 is evaluated again. The cycle continues until this condition is true, at which point control is transferred to the end of the PERFORM statement. If condition-1 is true at the beginning of execution of the PERFORM statement, control is transferred to the end of the PERFORM statement. See the diagram below.


        The VARYING Option of a PERFORM Statement with the TEST BEFORE Phrase Having One Condition

        When the data items referenced by two identifiers are varied, the content of the data item referenced by identifier-2 is set to literal-1 or the current value of the data item referenced by identifier-3 and then the content of the data item referenced by identifier-5 is set to literal-3 or the current value of the data item referenced by identifier-6. See the diagram of The VARYING Option of a PERFORM Statement with the TEST BEFORE Phrase Having Two Conditions below.

        After the contents of the data items referenced by the identifiers have been set, condition-1 is evaluated; if true, control is transferred to the end of the PERFORM statement; if false, condition-2 is evaluated. If condition-2 is false, the specified set of statements is executed once, then the content of the data item referenced by identifier-5 is augmented by literal-4 or the content of the data item referenced by identifier-7 and condition-2 is evaluated again.

        This cycle of evaluation and augmentation continues until this condition is true. When condition-2 is true, the content of the data item referenced by identifier-2 is augmented by literal-2 or the content of the data item referenced by identifier-4, the content of the data item referenced by identifier-5 is set to literal-3 or the current value of the data item referenced by identifier-6, and condition-1 is reevaluated. The PERFORM statement is completed if condition-1 is true; if not, the cycle continues until condition-1 is true.

        At the termination of the PERFORM statement, the data item referenced by identifier-5 contains literal-3 or the current value of the data item referenced by identifier-6. The data item referenced by identifier-2 contains a value that exceeds the last used setting by one increment or decrement value, unless condition-1 was true when the PERFORM statement was entered, in which case the data item referenced by identifier-2 contains literal-1 or the current value of the data item referenced by identifier-3.

        Figure 1. The VARYING Option of a PERFORM Statement with the TEST BEFORE Phrase Having Two Conditions
        The VARYING Option of a PERFORM Statement with the TEST BEFORE Phrase Having Two Conditions
      2. ANS85 If the TEST AFTER phrase is specified:

        When the data item referenced by one identifier is varied, the content of the data item referenced by identifier-2 is set to literal-1 or the current value of the data item referenced by identifier-3 at the point of execution of the PERFORM statement; then the specified set of statements is executed once and condition-1 of the UNTIL phrase is tested. If the condition is false, the value of the data item referenced by identifier-2 is augmented by the specified increment or decrement value (literal-2 or the value of the data item referenced by identifier-4) and the specified set of statements is executed again. The cycle continues until condition-1 is tested and found to be true, at which point control is transferred to the end of the PERFORM statement. See the diagram below.


        The VARYING Option of a PERFORM Statement with the TEST AFTER Phrase Having One Condition

        When the data items referenced by two identifiers are varied, the content of the data item referenced by identifier-2 is set to literal-1 or the current value of the data item referenced by identifier-3; then the current value of the data item referenced by identifier-5 is set to literal-3 or the current value of the data item referenced by identifier-6; and the specified set of statements is then executed. Condition-2 is then evaluated; if false, the content of the data item referenced by identifier-5 is augmented by literal-4 or the content of the data item referenced by identifier-7 and the specified set of statements is again executed. The cycle continues until condition-2 is again evaluated and found to be true, at which time condition-1 is evaluated. If false, the content of the data item referenced by identifier-2 is augmented by literal-2 or the content of data item referenced by identifier-4, the content of the data item referenced by identifier-5 is set to literal-3 or the current value of the data item referenced by identifier-6 and the specified set of statements is again executed. This cycle continues until condition-1 is again evaluated and found to be true, at which time control is transferred to the end of the PERFORM statement. See the diagram The VARYING Option of a PERFORM Statement with a TEST AFTER Phrase Having Two Conditions below.

        After completion of the PERFORM statement, each data item varied by an AFTER or VARYING phrase contains the same value it contained at the end of the most recent execution of the specified set of statements.

        Figure 2. The VARYING Option of a PERFORM Statement with a TEST AFTER Phrase Having Two Conditions
        The VARYING Option of a PERFORM Statement with a TEST AFTER Phrase Having Two Conditions

        During the execution of the specified set of statements associated with the PERFORM statement, any change to the VARYING variable (the data item referenced by identifier-2 and index-name-1), the BY variable (the data item referenced by identifier-4), the AFTER variable (the data item referenced by identifier-5 and index-name-3), or the FROM variable (the data item referenced by identifier-3 and index-name-2) is taken into consideration and affects the operation of the PERFORM statement.

        When the data items referenced by two identifiers are varied, the data item referenced by identifier-5 goes through a complete cycle (FROM, BY, UNTIL) each time the content of the data item referenced by identifier-2 is varied. When the contents of three or more data items referenced by identifiers are varied, the mechanism is the same as for two identifiers except that the data item being varied by each AFTER phrase goes through a complete cycle each time the data item being varied by the preceding AFTER phrase is augmented.

    5. NETJVM Format 5 is the PERFORM VARYING ... THROUGH. This variation of the PERFORM statement is used to set the values referenced by identifier-8 to access every element of collection during the execution of a PERFORM statement. The specified set of statements is performed once for each element of the collection referenced by identifier-9. Following the execution of the specified set of statements for each element of the collection, control is transferred to the end of the PERFORM statement. During execution of the PERFORM statement, reference to identifier-8 or identifier-9 cannot alter the number of times the specified set of statements is to be executed from that which was indicated by the initial size of the collection referenced by identifier-9.
  9. The range of a PERFORM statement consists logically of all those statements that are executed as a result of executing the PERFORM statement through execution of the explicit transfer of control to the end of the PERFORM statement. The range includes all statements that are executed as the result of a transfer of control by CALL, EXIT, GO TO and PERFORM statements in the range of the PERFORM statement, as well as all statements in declarative procedures that are executed as a result of the execution of statements in the range of the PERFORM statement. The statements in the range of a PERFORM statement need not appear consecutively in the source element.
  10. Statements executed as the result of a transfer of control caused by executing an EXIT PROGRAM statement are not considered to be part of the range of the PERFORM statement when both of the following conditions apply:
    1. The EXIT PROGRAM statement is specified in the same program in which the PERFORM statement is specified
    2. The EXIT PROGRAM statement is within the range of the PERFORM statement.
  11. If the range of a PERFORM statement includes another PERFORM statement, the sequence of procedures associated with the included PERFORM should itself either be totally included in, or totally excluded from, the logical sequence referred to by the first PERFORM. Thus, an active PERFORM statement, whose execution point begins within the range of another active PERFORM statement should not allow control to pass to the exit of the other active PERFORM statement; furthermore, two or more such active PERFORM statements should not have a common exit.

    MF These restrictions are not enforced. PERFORM statements can be freely nested, and recursion (a PERFORM statement performing a procedure containing it) is allowed. Only the exit point of the innermost PERFORM statement currently being executed is recognized. These rules can be changed by use of the PERFORM-TYPE Compiler directive.

    See the following illustrations for examples of legal PERFORM constructs.


    *

  12. A PERFORM statement that appears in a section that is not in an independent segment can have within its range, in addition to any declarative sections whose execution is caused within that range, one of the following:
    1. Sections and/or paragraphs wholly contained in one or more non-independent segments.
    2. Sections and/or paragraphs wholly contained in a single independent segment.

    OSVSVSC2 These restrictions do not apply.

  13. A PERFORM statement that appears in an independent segment can have within its range, in addition to any declarative sections whose execution is caused within that range, only one of the following:
    1. Sections and/or paragraphs wholly contained in one or more non-independent segments.
    2. Sections and/or paragraphs wholly contained in the same independent segment as that PERFORM statement.

    OSVSVSC2 These restrictions do not apply.

General Rules for Format 4

  1. The data items referenced by identifier-4 and identifier-7 must not have a zero value.
  2. If an index-name is specified in the VARYING or AFTER phrase, and an identifier is specified in the associated FROM phrase, the data item referenced by the identifier must have a positive value.
Previous Topic Next topic Print topic