PERFORM Statement [ HP COBOL II/XL Reference Manual ] MPE/iX 5.0 Documentation
HP COBOL II/XL Reference Manual
PERFORM Statement
The PERFORM statement transfers control explicitly to one or more
procedures, and implicitly returns control to the statement after the
current PERFORM statement when execution of the specified procedure or
procedures 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.
These parameters are used by the four general formats of the PERFORM
statement.
Parameters
procedure- names of procedures within the PROCEDURE DIVISION of the
name-1 and program in which the PERFORM statement appears. If one
procedure- of the procedures is a declarative procedure, then both
name-2 must be declarative.
identifier-1 numeric elementary items described in the DATA DIVISION.
through
identifier-7
literal-1 numeric literals.
through
literal-4
condition-1 any valid COBOL condition.
and
condition-2
THRU equivalent to THROUGH.
END-PERFORM terminates the in-line PERFORM statement.
Syntax
Format 1 of the PERFORM statement is the basic PERFORM statement. The
specified set of statements is executed once as described on the
preceding pages. Control then passes to the next executable statement
following the PERFORM statement.
A format 2 PERFORM statement allows you to perform the specified set of
statements the number of times specified by integer-1 or the numeric
integer named by identifier-1. Following the execution of the
statements, control is passed to the next executable statement following
the PERFORM statement.
If identifier-1 is a negative integer or zero (0) when the PERFORM
statement is executed, control immediately passes to the next executable
statement following the PERFORM statement. Thus, a negative integer or
zero value for identifier-1 is equivalent to not having the PERFORM
statement in the code at the time of execution.
If identifier-1 is referenced during execution of the PERFORM statement,
it does not change the number of times the specified set of statements is
executed, as indicated by the initial value of identifier-1.
A format 3 PERFORM statement uses condition-1 to control the number of
times the specified set of statements is performed. The statements are
performed as long as condition-1 is false.
When condition-1 is true, control passes to the next executable statement
after the PERFORM.
NOTE Be sure that, within the specified set of statements, condition-1
eventually has a value of true.
When TEST BEFORE is specified: The condition is checked before the
specified set of statements is performed (the set of statements will be
performed 0 or more times).
When TEST AFTER is specified: The condition is checked after the
specified set of statements has been performed (the statements are
always performed at least once).
If neither TEST BEFORE nor TEST AFTER is specified, the TEST BEFORE
phrase is assumed.
Format 4 PERFORM Statement (PERFORM...VARYING)
A PERFORM...VARYING 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.
For clarity, two versions of format 4 are presented below: the first is
for an out-of-line PERFORM and the second is for an in-line PERFORM.
[REV BEG]
Up to six AFTER phrases can be[REV END] specified in format 4. However,
if procedure-name-1 is omitted, the AFTER phrase cannot be specified.
Any literal used in the BY phrase, and data items referenced by
identifier-4, identifier-7, and identifier-10 must not have a value of
zero. Also, if an index name is used in the VARYING or AFTER phrase,
then the following is true:
* If an identifier or a literal is specified in the associated FROM
phrase, the data item referenced by the identifier or the value of
the literal must be a positive integer.
* If an identifier is specified in the associated BY phrase, it must
name an integer data item. If a literal is specified, it must be
a positive integer.
If an index name is specified in the FROM phrase, the following is true:
* If an identifier is used in the associated VARYING or AFTER
phrase, it must name an integer data item.
* If an identifier or literal is used in the associated BY phrase,
the literal or the data item referenced by the identifier must be
an integer.
When an index name appears in a VARYING or an AFTER phrase, it is
initialized and subsequently augmented according to the rules of the SET
statement. When an index name appears in a FROM phrase, any identifier
appearing in an associated VARYING or AFTER phrase is initialized
according to the rules of the SET statement. It is subsequently
augmented using the SET statement rules as described below.
Variation of a Single Identifier
Variation of a single identifier is accomplished by using a format 4
PERFORM statement of the following form:
Out-of-Line PERFORM
When a PERFORM statement is executed, control is transferred to the first
statement of the procedure named procedure-name-1. This transfer occurs
only once for each execution of a PERFORM statement.
An implicit transfer of control to the next executable statement
following the PERFORM statement is established as follows:
* If procedure-name-1 is a paragraph name and procedure-name-2 is
not specified, then the return occurs after the last statement of
procedure-name-1.
* If procedure-name-1 is a section name and procedure-name-2 is is
not specified, then the return occurs after the last statement of
the last paragraph of the section named by procedure-name-1.
* If procedure-name-1 and procedure-name-2 are specified, and
procedure-name-2 is a paragraph name, the return occurs after the
last statement of procedure-name-2.
* If procedure-name-2 is specified, and is the name of a section,
the return occurs after the last statement of the last paragraph
in the section named by procedure-name-2.
No relationship is necessary between procedure-name-1 and
procedure-name-2, except that a consecutive sequence of operations is to
be executed beginning at procedure-name-1, and ending with the execution
of procedure-name-2.
If a procedure within the range of a format 2 PERFORM statement is
contained in a section (or is a section) whose section number is greater
than 49, the section in which it is contained is in its initial state
only the first time the section is entered. Each time the section is
subsequently entered as a result of the PERFORM statement, it is in its
last used state. After the procedure has been executed the specified
number of times, it is entered in its initial state the next time the
procedure is referenced.[REV BEG] The term initial state refers to the
original setting of GO TO statements before they are modified at run time
by the ALTER statement (refer to "ALTER Statement" in Chapter 9 ).[REV
END]
The following is an example of an out-of-line PERFORM statement:
PERFORM READ-INPUT WITH TEST AFTER
UNTIL EOF-FLAG="YES"
The above example performs READ-INPUT at least once.
In-Line PERFORM
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.
When an in-line PERFORM statement is executed, control is transferred to
the first statement of imperative-statement-1. This transfer occurs only
once for each execution of a PERFORM statement.
Following is an example of an in-line PERFORM statement:
PERFORM
MOVE A TO B
ADD 1 TO B
END-PERFORM.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 4
WRITE P-FILE FROM HEADING(I)
END-PERFORM.
General Rules of PERFORM
The following rules apply to all four formats of the PERFORM statement.
* 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 those statements contained within an in-line
PERFORM statement itself, are referred to as the specified set of
statements.
* 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.
Range of the PERFORM Statement
The range of the PERFORM statement includes all statements that are
executed as a result of executing the PERFORM statement through an
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 within
the range of the PERFORM statement. The range also includes all
statements in declarative procedures that are executed as a result of
executing statements in the range of the PERFORM statement. It is not
necessary that the statements in the range of a PERFORM statement appear
in consecutive order in the source program.
Statements executed as the result of a transfer of control caused by
executing an EXIT PROGRAM statement are not considered to be a part of
the range of the PERFORM statement when the following is true:
* The EXIT PROGRAM statement is specified in the same program in
which the PERFORM statement is specified.
* The EXIT PROGRAM statement is within the range of the PERFORM
statement.
A PERFORM statement that appears in an independent program segment can
have within its range, in addition to any declarative segment executed
within that range, only one of the following:
* Sections or paragraphs wholly contained in one or more
nonindependent segments.
* Sections or paragraphs wholly contained in the same independent
segment as that PERFORM statement.
A PERFORM statement that appears in a nonindependent program segment can
have within its range, in addition to the declarative sections executed
within that range, only one of the following:
* Sections or paragraphs wholly contained in one or more
nonindependent segments.
* Sections or paragraphs wholly contained in a single independent
segment.
Nested PERFORM Statements
If a procedure or sequence of procedures contains another PERFORM
statement, the procedure(s) associated with the nested PERFORM statement
must be totally included in, or totally excluded from, the procedures
referenced by the first PERFORM statement.
PERFORM Constructs
Figure 9-1 gives three illustrations of valid PERFORM constructs.
Figure 9-1. Valid PERFORM Constructs
NOTE Refer to "MPE XL System Dependencies" in Appendix H for more
information on the PERFORM statement.
When the TEST BEFORE phrase is specified or implied:
Parameter-1 is set to the value of parameter-2 when the PERFORM
statement is initially executed. If condition-1 is true, control is
transferred to the next executable statement following the PERFORM
statement. Otherwise, the specified set of statements is executed
once.
The value of parameter-1 is then augmented by the increment or
decrement value specified by parameter-3, and condition-1 is evaluated.
If condition-1 is false, the cycle of executing the specified set of
statements, augmenting parameter-1, and evaluating condition-1 is
repeated. This cycle continues until condition-1 is true. At this
time, control is passed to the first executable statement after the
PERFORM statement.
The flowchart in Figure 9-2 illustrates variation of a single
identifier when TEST BEFORE is specified.
Figure 9-2. Variation of a Single Identifier with TEST BEFORE
When the TEST AFTER phrase is specified:
Parameter-1 is set to the current value of parameter-2 when the PERFORM
statement is executed. Then, the specified set of statements is
executed once and condition-1 is evaluated. If condition-1 is false,
parameter-1 is augmented by the specified increment or decrement value
of parameter-3 and the specified set of statements is executed again.
The cycle continues until condition-1 is evaluated and found to be
true. Control is then transferred to the end of the PERFORM statement.
The flowchart in Figure 9-3 illustrates variation of a single
identifier when TEST AFTER is specified.
Figure 9-3. Variation of a Single Identifier with TEST AFTER
Variation of Two or More Identifiers.
Variation of two or more identifiers is accomplished using format 4 of
the PERFORM statement in the following form:
NOTE The format indicates that variation of two or more identifiers only
applies to the out-of-line PERFORM; AFTER phrases are not included
in the in-line PERFORM.
There are three cases of the PERFORM...VARYING statement with two or more
identifiers. The first case conforms to ANSI COBOL'74, and the other two
cases conform to ANSI COBOL'85.
NOTE The latter two forms of the PERFORM...VARYING statement are
incompatible with ANSI COBOL'74. For compatibility with ANSI
COBOL'74, use the ANSI74 entry point.
ANSI COBOL'74.
In this case, parameter-1 and parameter-4 are set to the current values
of parameter-2 and parameter-5 respectively. Following this, condition-1
is tested and if true, causes the PERFORM statement to cease execution.
Control is transferred to the next executable statement following the
PERFORM statement. If condition-1 is false, condition-2 is tested with
the same consequences as condition-1 if the result is true.
If condition-2 is false, the specified procedures are executed once,
parameter-4 is augmented by parameter-6 and condition-2 is tested. This
cycle continues until condition-2 is true. Then parameter-4 is set to
the value of parameter-5, parameter-1 is augmented by parameter-3 and
condition-1 is evaluated again. If condition-1 is true, the statement is
complete. If it is false, the cycle using parameter-4 and condition-2 is
repeated.
These two cycles are repeated until, after the cycle using parameter-4
and condition-2 is complete, condition-1 is true.
During execution of the procedures, any change in the values of
parameter-1, parameter-2, or parameter-4 is taken into consideration, and
affects the operation of the PERFORM statement.
At the end of this type of PERFORM statement, parameter-4 has the current
value of parameter-5, and parameter-1 differs in value from its last used
setting by the value of parameter-3. This is always true except when
condition-1 is true initially. In this case, parameter-1 has the value
of parameter-2.
The flowchart in Figure 9-4 illustrates PERFORM...VARYING with two
conditions.
Figure 9-4. Variation of Two Conditions (ANSI COBOL'74)
ANSI COBOL'85.
When TEST BEFORE is specified: Parameter-1 and parameter-4 are
set to parameter-2 and parameter-5. Afterward, condition-1 is
evaluated. If the condition is 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 parameter-4 is augmented by parameter-6, and condition-2 is
evaluated again. This cycle of evaluation and augmentation continues
until the condition is true. When condition-2 is true,
parameter-1 is augmented by parameter-3, parameter-4 is set to
parameter-5, 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.
Figure 9-5 illustrates the PERFORM...VARYING statement with the TEST
BEFORE phrase having two conditions.
Figure 9-5. Variation of Two Conditions with TEST BEFORE (ANSI COBOL'85)
When TEST AFTER is specified:
Parameter-1 and parameter-4 are set to parameter-2 and parameter-5, and
the specified set of statements is executed. Condition-2 is then
evaluated; if false, parameter-4 is augmented by parameter-6, 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 condition-1 is false, parameter-1 is augmented by parameter-3. Also,
parameter-4 is set to parameter-5, 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.
After the 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 9-6 illustrates the PERFORM...VARYING statement with the TEST
AFTER phrase having two conditions.
Figure 9-6. Variation of Two Conditions with TEST AFTER (ANSI COBOL'85)
Variation of More than Two Identifiers.
When 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. The only
exception is 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. Thus the last AFTER phrase varies
fastest.
Incompatibility Between ANSI COBOL'74 and ANSI COBOL'85
The order of steps in a multi-conditional PERFORM...VARYING statement has
been changed. This change creates an incompatibility when there is a
dependence between identifier-2 and identifier-5. The following example
illustrates this difference:
PERFORM PARA3 VARYING X FROM 1 BY 1 UNTIL X IS GREATER THAN 3
AFTER Y FROM X BY 1 UNTIL Y IS GREATER THAN 3.
Under ANSI COBOL'74, PARA3 is executed eight times with the following
values:
X: 1 1 1 2 2 2 3 3
Y: 1 2 3 1 2 3 2 3
Under ANSI COBOL'85, PARA3 is executed six times with the following
values:
X: 1 1 1 2 2 3
Y: 1 2 3 2 3 3
One would expect the above example to perform the same as the example
below:
PERFORM PARA2 VARYING X FROM 1 BY 1 UNTIL X IS GREATER THAN 3.
PARA2.
PERFORM PARA3 VARYING Y FROM X BY 1 UNTIL Y IS GREATER THAN 3.
Under ANSI COBOL'74, PARA3 is executed eight times, whereas under ANSI
COBOL'85, PARA3 is executed six times, as shown above.
Examples
The following program fragment shows several examples of PERFORM
statements.
01 DISKIN-RECORD.
03 DI-DATA OCCURS 4 TIMES.
05 DI-NAME PIC X(20).
05 DI-ADDRESS PIC X(20).
05 DI-CTY-ST PIC X(20).
05 DI-ZIP PIC 9(05).
05 DI-AMOUNT PIC 9(03)V99 OCCURS 3 TIMES.
01 WS-FILE-CTR.
03 WS-AMOUNT PIC 9(06)V99 COMP VALUE 0.
03 SUB-1 PIC 9(01).
03 SUB-2 PIC 9(01).
03 WS-DISKIN-CTRL PIC 9(01) VALUE 0.
88 WS-DISKIN-TO-OPEN VALUE 0.
88 WS-DISKIN-OPEN VALUE 1.
88 WS-DISKIN-EOF VALUE 2.
PROCEDURE DIVISION.
200-PROGRAM-START.
** EXAMPLE OF PERFORM USING THRU OPTION ******************
PERFORM 300-GET-DISKIN-RTN THRU 300-GET-DISKIN-EXIT.
** EXAMPLE OF PERFORM USING UNTIL OPTION *****************
PERFORM 250-PROCESS-RECORD UNTIL WS-DISKIN-EOF.
STOP RUN.
250-PROCESS-RECORD.
** EXAMPLE OF PERFORM USING VARYING & UNTIL OPTIONS ******
PERFORM 260-UNBLOCK-RECORD
VARYING SUB-1 FROM 1 BY 1
UNTIL SUB-1 GREATER 5.
260-UNBLOCK-RECORD.
MOVE DI-NAME (SUB-1) TO P-NAME.
MOVE DI-ADDRESS (SUB-1) TO P-ADDRESS.
MOVE DI-CTY-ST (SUB-1) TO P-CTY-ST.
MOVE ZERO TO SUB-2.
** EXAMPLE OF PERFORM USING # OF TIMES OPTION ************
PERFORM 270-ACCUMULATE-AMOUNTS 3 TIMES.
270-ACCUMULATE-AMOUNTS.
ADD 1 TO SUB-2.
ADD DI-AMOUNT (SUB-1,SUB-2)TO WS-AMOUNT.
300-GET-DISKIN-RTN.
IF WS-DISKIN-TO-OPEN
OPEN INPUT DISKIN-FILE
MOVE 1 TO WS-DISKIN-CTRL.
READ DISKIN-FILE
AT END
CLOSE DISKIN-FILE
MOVE 2 TO WS-DISKIN-CTRL.
300-GET-DISKIN-EXIT.
EXIT.
MPE/iX 5.0 Documentation