PERFORM Format 1 – Procedural

6.32.1. PERFORM Format 1 – Procedural

Figure 6-75 - Procedural PERFORM Syntax This format of the

PERFORM procedure-name-1 [ THROUGH

PERFORM statement is

THRU

procedure-name-2 ]

used to transfer control to

FOREVER

one or more procedures literal-1

and to return control when

identifier-1 execution of the specified procedure(s) is complete.

TIMES

WITH TEST BEFORE AFTER

This invocation of the procedure(s) can be done

VARYING identifier-3 FROM

a single time, multiple

times, repeatedly until a UNTIL conditional-expression -1

identifier-4

identifier-5

condition becomes TRUE

or forever (with –

AFTER identifier-4 FROM

presumably – some way of

UNTIL conditional-expression -2

breaking out of the control of the PERFORM within the

procedure(s).

1. The words THROUGH and THRU may be used interchangeably.

2. Both procedure-name-1 and procedure-name-2 must be PROCEDURE DIVISION sections or paragraphs defined in

the same program unit as the PERFORM statement.

3. If procedure-name-2 option is specified, it must follow procedure-name-1 in the program’s source code.

4. The scope of the PERFORM is defined as being the statements within procedure-name-1, the statements within

procedure-name-2 and all statements in all procedures defined between them.

5. Without the FOREVER, TIMES or UNTIL clauses, the code within the scope of the PERFORM will be executed (once) and control will return to the statement following the PERFORM.

6. The FOREVER option will repeatedly execute the code within the scope of the PERFORM with no conditions

defined on the PERFORM statement itself for termination of the repetition. It will be up to the programmer to include code within the scope of the PERFORM that will either halt the program (STOP RUN) or break out of the PERFORM (EXIT PERFORM).

7. The TIMES option will repeat the execution of the instructions within the scope of the PERFORM a fixed number of times. Once that number of repetitions has concluded, control will fall into the next statement following the PERFORM.

8. The UNTIL clause will enable the statements within the scope of the PERFORM to be executed repeatedly until

such time as the value of conditional-expression-1 becomes TRUE.

9. The optional WITH TEST clause will control whether the UNTIL test is performed BEFORE the scope of the PERFORM is executed or AFTER. The default, if no WITH TEST clause is specified, is BEFORE.

10. The optional VARYING clause allows for the definition of a data item (identifier-3) that will have a unique numeric value for each iteration of the execution of the statements within the scope of the PERFORM. The first time, identifier-3 will have the value specified by the FROM clause. At the conclusion of each iteration, the value defined by the BY clause will be added to identifier-3 before conditional-expression-1 is evaluated. The default BY value, if no BY clause is specified, is 1.

11. If a VARYING clause has been used, you may also use any number of additional AFTER clauses to create a secondary loop situation where each AFTER will create an additional series of iterations, will define an additional data item to be incremented during each iteration and will define an additional conditional expression to define the termination of that series of iterations. Functionally, this is basically a way of nesting a

PERFORM/VARYING/UNTIL within another PERFORM/VARYING/UNTIL without the need to code multiple statements. An example will probably help.

Observe the following code which defines a two- dimensional (3 row by 4 column) table and a pair of

PD (1, 3) PD (1, 4) numeric data items to be used to subscript

PD (1, 1)

PD (1, 2)

references to each element of the table:

PD (2, 1)

PD (2, 2)

PD (2, 3) PD (2, 4)

01 PERFORM-DEMO. 05 PD-ROW OCCURS 3 TIMES. 10 PD-COL OCCURS 4 TIMES

PD (3, 1)

PD (3, 2)

PD (3, 3) PD (3, 4)

15 PD PIC X(1). 01 PD-Col-No PIC 9 COMP. 01 PD-Row-No PIC 9 COMP.

Let’s say we want to PERFORM a routine (100-Visit-Each-PD) which will – in turn – access each PD data item in the sequence shown to the right. Here’s the PERFORM

1 2 3 4 code: PERFORM 100-Visit-Each-PD WITH TEST AFTER

VARYING PD-Row-No FROM 1 BY 1 UNTIL PD-Row-No = 3 AFTER PD-Col-No FROM 1 BY 1 UNTIL PD-Col-No = 4.

But, perhaps you needed to “visit” each PD in the

1 4 7 10 sequence shown to the left. If so, then here’s the PERFORM you need:

2 PERFORM 100-Visit-Each-PD WITH TEST AFTER 5 8 11

VARYING PD-Col-No FROM 1 BY 1 UNTIL PD-Col-No = 4 VARYING PD-Row-No FROM 1 BY 1 UNTIL PD-Row-No = 3.