Friday, September 4, 2009

The PERFORM Statement
Syntax
Format 1
PERFORM [procedure-name-1 [{ THROUGH
THRU } procedure-name-2 ] ]
[imperative-statement-1 END-PERFORM]
Format 2
PERFORM [procedure-name-1 [{ THROUGH
THRU } procedure-name-2 ] ]
{ identifier-1
integer-1 } TIMES [imperative-statement-1 END-PERFORM]
Format 3
PERFORM [procedure-name-1 [{ THROUGH
THRU } procedure-name-2 ] ]
[ WITH TEST { BEFORE
AFTER }] UNTIL condition-1
[imperative-statement-1 END-PERFORM]
Format 4
PERFORM [procedure-name-1 [{ THROUGH
THRU } procedure-name-2 ] ]
[ WITH TEST { BEFORE
AFTER }]
VARYING { identifier-2
index-name-1 } FROM { identifier-3
index-name-2
literal-1 } BY { identifier-4
literal-2 }
UNTIL condition-2
[AFTER { identifier-4
index-name-3 } FROM { identifier-5
index-name-4
literal-3 } BY { identifier-5
literal-2 }
UNTIL condition-3] ...
[imperative-statement-1 END-PERFORM]



Description
The PERFORM statement is used to execute code. There are two different modes of operation.
The first mode is used to execute code in another area of the program and is invoked by mentioning a procedure or paragraph name. In this mode, the code in the other paragraph(s) is executed and control is returned to the statement following the PERFORM statement.
The second mode is called an in-line PERFORM, because the code to be executed is actually coded in place inside of the PERFORM statement. In the second mode, the code which is executed is that which lies between the PERFORM and the END-PERFORM.
Each of the four formats works slightly differently and is described in the correspondingly numbered area.
1. The first format is used to execute the referenced code one time.
2. The second format is used to execute the referenced code multiple times. The actual number of times is indicated by identifier-1 or integer-1.
3. The third format is also used to execute the referenced multiple times. The actual number of times is dependent upon condition-1. The code will be repeatedly executed until condition-1 is true. The condition will be tested before the code is actually tested unless the WITH TEST AFTER phrase is included.
4. The fourth format is used to execute the referenced multiple times with the actual number of times being dependent upon a condition like the third format. The primary difference between format 3 and format 4 is the VARYING phrase, which is used to count in a variable during the repetitions. The VARYING variable is set to the FROM value before the referenced code is executed. Before each subsequent pass, the VARYING variable is incremented by the amount of the BY value. The optional AFTER phrase may be included to vary the value of an additional variable. If this option is used, AFTER variable will be incremented through its entire cycle for each time the VARYING variable is cycled.

Tips
1. Use the PERFORM statement to access code which needs to be executed from more than one location.
2. Use the PERFORM statement to structure your code. Make a main routine that PERFORMs other routines which in turn PERFORM other routines which themselves PERFORM other routines until the level of complexity of the PERFORMed routines is easily comprehended.
3. Use the WITH TEST AFTER option to force the code to be PERFORMed at least once.
4. Use the VARYING ... AFTER option to pass through all the entries in multiple dimensional tables.
5. If the number of lines in the routine is relatively small, use an in-line PERFORM instead of PERFORMing a separate paragraph. The next programmer will appreciate not having to look all through the program to find the one or two lines of code in the paragraph you PERFORMed