PERFORM Statement COBOL

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. The PERFORM statement is also used to control execution of one or more imperative statements that are within the scope of that PERFORM statement.

Format 1:Perform (Once)

Perform (Once)

Format 2:Perform…Times

Perform…Times

Format 3:Perform…Until

Perform…Until

Format 4:Perform…Varying

Perform…Varying

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.

If neither the TEST BEFORE nor the TEST AFTER phrase is specified, the TEST BEFORE phrase is assumed.

Each identifier represents a numeric elementary item described in the Data Division.In Format 2, identifier-1 must be described as a numeric integer.

Each literal represents a numeric literal.

The words THRU and THROUGH are synonymous.

If an index-name is specified in the VARYING or AFTER phrase, then:

  • The identifier in the associated FROM and BY phrases must reference an integer data item.
  • The literal in the associated FROM phrase must be a positive integer.
  • The literal in the associated BY phrase must be a nonzero integer.
  • If an index-name is specified in the FROM phrase, then:
  • The identifier in the associated VARYING or AFTER phrase must refer to an integer data item.
  • The identifier in the associated BY phrase must refer to an integer data item.
  • The literal in the associated BY phrase must be an integer.

literal-2 or literal-4 in the BY phrase must not be zero.

condition-1, condition-2, . . . may be any conditional expression.

When 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.

The data items referenced by identifier-4 and identifier-7 must not have a zero value.

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.

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

The statements contained within the range of procedure-name-1 (through procedure-name-2 , if specified) for an out-of-line PERFORM statement or contained within the PERFORM statement itself for an in-line PERFORM statement are referred to as the specified set of statements.

The END-PERFORM phrase delimits the scope of the in-line PERFORM statement.

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 terms in-line or out-of-line, all the rules that apply to the out-of-line PERFORM statement also apply to the in-line PERFORM statement.

When the PERFORM statement is executed, control is transferred to the first statement of the specified set of statements, except as indicated in the general rules for Formats 2 through 4 as given below. This transfer of control occurs only once for each execution of a PERFORM statement. For those cases when 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:

  • 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.
  • 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.
  • If procedure-name-2 is specified and it is a paragraph-name, the return is after the last statement of the paragraph.
  • 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 section.
  • 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.

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 TO and PERFORM statements may occur between procedure-name-1 and the end of procedure-name-2. If there are two or more logical paths to the return point, procedure-name-2 may be the name of a paragraph consisting of the EXIT or CONTINUE statement, to which all of these paths must lead.

If control passes to the specified set of statements by means other than a PERFORM statement, control will pass through the last statement of the set to the next executable statement as if no PERFORM statement referenced the set.

The PERFORM statements operate as described in the following paragraphs. 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.

Format 2 is the PERFORM . . . TIMES statement.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 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, references 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.

Format 3 is the PERFORM . . . UNTIL statement. 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 and the TEST AFTER phrase is not specified, control passes to the end of the PERFORM statement and the specified set of statements is not executed. In the absence of the TEST AFTER phrase (that is, TEST BEFORE is specified or implied), testing of the specified condition occurs before each execution of the specified set of statements. When the TEST AFTER phrase is specified, the specified set of statements is executed before the specified condition is tested. Any subscripting or reference modification associated with the operands in condition-1 is evaluated each time the condition is tested.

Format 4 is the PERFORM . . . VARYING statement. This variation of the PERFORM statement is used to augment the values referred to 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 or index-name-3 is specified, the value of the associated index at the beginning of the PERFORM statement must be set to an occurrence number of an element in the table. If index-name-2 or index-name-4 is specified, the value of the data item referred to by identifier-2 or identifier-5 at the beginning of the PERFORM statement must be equal to an occurrence number of an element in a table associated with index-name-2 or index-name-4. 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 may 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 contents of the data item referred to by the identifier are set or augmented. If identifier-3, identifier-4, identifier-6 or identifier-7 is subscripted, the subscripts are evaluated each time the contents of the data item referred to by the identifier are 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.

The following paragraphs specify in detail the actions that occur as a result of executing four of the simpler forms of a PERFORM . . . VARYING statement. The actions that occur when more complex forms of the PERFORM . . . VARYING statement are executed may be inferred by generalization from the simpler forms.

For a PERFORM . . . VARYING statement that does not have an AFTER phrase nor a TEST AFTER phrase (that is, TEST BEFORE is specified or implied), the data item referred to by identifier-2 is set to literal-1 or the current value of the data item referred to by identifier-3 at the point of initial execution of the statement; then condition-1 in the UNTIL phrase is tested. If it is false, the specified set of statements is executed once. The value of the data item referred to by identifier-2 is augmented by the specified increment or decrement value (literal-2 or the value of the data item referred to by identifier-4 in the BY phrase) and condition-1 is retested, with subsequent execution of the specified set of statements if it is found to be false. The cycle continues until condition-1 is found to be true, at which time 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 without executing the specified set of statements at all.

Figure represents this sequence of actions.

PERFORM . . . VARYING Statement

PERFORM . VARYING Statement

When control reaches the end of this form of the PERFORM . . . VARYING statement, the data item referred to by identifier-2 contains a value that exceeds the setting last used by one increment or decrement value, unless condition-1 was true to begin with, in which case it contains literal-1 or the current value of the data item referred to by identifier-3.

For a PERFORM . . . VARYING statement that has one AFTER phrase but no TEST AFTER phrase (that is, TEST BEFORE is specified or implied), the data item referred to by identifier-2 is set to literal-1 or to the current value of the data item referred to by identifier-3; then the data item referred to by identifier-5 is set to literal-3 or to the current value of the data item referred to by identifier-6. Subsequent actions form a nested set of two cycles. condition-1 is tested. If it is true, control is transferred to the end of the PERFORM statement; if it is false, condition-2 is tested. If condition-2 is false, the specified set of statements is executed once, then the data item referred to by identifier-5 is augmented by literal-4 or by the current value of the data item referred to by identifier-7, and condition-2 is retested with subsequent execution of the specified set of statements if it is found to be false. This inner cycle of execution, testing, and augmentation continues until condition-2 is found to be true, at which time the data item referred to by identifier-2 is augmented by literal-2 or by the current value of the data item referred to by identifier-4, identifier-5 is set to literal-3 or to the current value of the data item referred to by identifier-6, and condition-1 is retested with subsequent reevaluation of condition-2 as long as condition-1 is found to be false. This outer cycle continues until condition-1 is found to be true.

Figure represents this sequence of actions.

PERFORM . . . VARYING Statement

PERFORM . . . VARYING Statement

When control reaches the end of this form of the PERFORM . . . VARYING statement, the data item referred to by identifier-5 contains literal-3 or the current value of the data item referred to by identifier-6. The data item referred to by identifier-2 contains a value that exceeds the last used setting by one increment or decrement value, unless condition- 1 was true to begin with, in which case it contains literal-1 or the current value of the data item referred to by identifier-3.

For a PERFORM . . . VARYING statement that does not have an AFTER phrase but does have a TEST AFTER phrase, the data item referred to by identifier-2 is set to literal-1 or the current value of the data item referred to by identifier-3 at the point of initial execution of the statement; then the specified set of statements is executed once and condition-1 in the UNTIL phrase is tested. If it is false, the value of the data item referred to by identifier-2 is augmented by the specified increment or decrement value (literal-2 or the value of the data item referred to by identifier-4) and the specified set of statements is re-executed with subsequent reevaluation of condition-1. The cycle continues until condition-1 is found to be true, at which time control is transferred to the end of the PERFORM statement. For this form of the PERFORM... VARYING statement, the specified set of statements is always executed at least once.

Figure represents this sequence of actions.

PERFORM . . . VARYING Statement

PERFORM . . . VARYING Statement

When control reaches the end of this form of the PERFORM . . . VARYING statement, identifier-2 contains the same value it contained at the end of the most recent execution of the specified set of statements.

For a PERFORM . . . VARYING statement that has one AFTER phrase and a TEST AFTER phrase, the data item referred to by identifier-2 is set to literal-1 or the current value of the data item referred to by identifier-3, then the data item referred to by identifier-5 is set to literal-3 or the current value of the data item referred to by identifier-6. The specified set of statements is executed once and condition-2 is tested. If it is false, the data item referred to by identifier-5 is augmented by literal-4 or the current value of the data item referred to by identifier-7 and the specified set of statements is reexecuted with subsequent reevaluation of condition-2. This inner cycle of execution, testing and augmentation continues until condition-2 is found to be true, at which time condition-1 is tested. If it is true, control is transferred to the end of the PERFORM statement. If it is false, the data item referred to by identifier-2 is augmented by literal-2 or the current value of the data item referred to by identifier-4. identifier-5 is set to literal-3 or the current value of the data item referred to by identifier-6; and the specified set of statements is reexecuted with subsequent reevaluation of condition-2. This outer cycle continues until both condition-1 and condition-2 are found to be true.

PERFORM . . . VARYING Statement

PERFORM . . . VARYING Statement

When control reaches the end of this form of the PERFORM . . . VARYING 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.

The preceding definition of the operation of a Format 4 PERFORM statement complies with ANSI COBOL 1985. It should be noted that this definition differs slightly from the definition in ANSI COBOL 1974, with which earlier versions of RM/COBOL complied. The difference is in the point at which inner cycle loop variables are reset to their FROM values.

It can have an effect only on Format 4 PERFORM statements that specify one or more AFTER phrases and that specify an inner FROM operand that is dependent on one of the higher-level loop operands. Most Format 4 PERFORM statements are not of this form, and are, therefore, not affected by this change. In situations where it is necessary to preserve compatibility with earlier versions of COBOL in this regard, two courses of action are possible:either modify the text of the source program, replacing the Format 4 PERFORM statement with an appropriate sequence of IF, MOVE, ADD and Format 1 PERFORM statements; or make use of the Compile Command option that causes the RM/COBOL compiler to treat Format 4 PERFORM statements as before. The RM/COBOL User’s Guide contains further information on this option and the language features it controls.

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

When the data items referred to by two identifiers are varied, the data item referred to by identifier-5 goes through a complete cycle (FROM, BY, UNTIL) each time the contents of the data item referred to by identifier-2 are varied. When the contents of three or more data items referred to 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.

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 implicit 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 program.

If the specified set of statements for one PERFORM statement includes another PERFORM statement, the specified set of statements associated with the inner PERFORM must itself be either totally included in, or totally excluded from, the logical sequence referred to by the outer PERFORM statement.Thus an active PERFORM statement, whose execution point begins within the range of another active PERFORM statement, must not allow control to pass to the exit of the other active PERFORM statement; furthermore, two or more such active PERFORM statements may not have a common exit.This is illustrated in the following figures

PERFORM Statement Examples

PERFORM Statement Examples

PERFORM Statement Examples

PERFORM Statement Examples

PERFORM Statement Examples

PERFORM Statement Examples

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, only one of the following:

  • Sections, paragraphs, or both, wholly contained in one or more nonindependent segments.
  • Sections, paragraphs, or both, wholly contained in a single independent segment.

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:

  • Sections, paragraphs, or both, wholly contained in one or more nonindependent segments.
  • Sections, paragraphs, or both, wholly contained in the same independent segment as the PERFORM statement.

PERFORM Statement Examples


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics