PERFORM Statement
The PERFORM statement executes a series of statements iteratively.
Format 1
A Format 1 PERFORM statement executes a procedure, or the code beginning in a proc-1 up to, and including the code in a named proc-2 once, or multiple times as designated by the FOREVER phrase or the TIMES phrase.
PERFORM [proc-1 [ THRU proc-2 ] ]
[ { FOREVER } ]
{numeric-1 TIMES }
Syntax:
proc-nis a procedure or section declared in the program.numeric-nis a numeric literal or data item.num-nis a numeric literal or data item.
General Rules:
THRUandTHROUGHare synonyms- When the
THRUstatement is not used, the scope of the Format 1PERFORMstatement is all of the statements withinproc-1. - When the
THRUstatement is used, the scope of the Format 1PERFORMstatement is all of the statements withinproc-1, all of the statements betweenproc-1andproc-2, and all of the statements inproc-2. - In a
proc-1thruproc-2construct,proc-2must followproc-1in the source code. - The
TIMESphrase indicates that the number of times that statements within the scope of thePERFORMstatement are to be executed. After the statements within the scope of thePERFORMstatement have been executed the designated number ofTIMES, control returns to the next statement in the program after thePERFORMstatement. - The
FOREVERphrase indicates that the statements within the scope of thePERFORMstatement should continue to execute continuously. A program may exit from aPERFORM FOREVERphrase with anEXIT PERFORM CYCLEstatement.
Format 2
A Format 2 PERFORM statement executes a procedure, or the code beginning in a proc-1 up to, and including the code in a named proc-2 UNTIL condition-1 tests true.
PERFORM [proc-1 [ THRU proc-2 ] ]
[WITH TEST {BEFORE}]
{AFTER }
[VARYING { num-1 FROM num-2 by num-3 } ] UNTIL condition-1
Syntax:
proc-nis a procedure or section declared in the program.num-nis a numeric literal or data item.condition-1is a conditional statement.
General Rules:
THRUandTHROUGHare synonyms.- When the
THRUstatement is not used, the scope of the Format 2PERFORMstatement is all of the statements withinproc-1. - When the
THRUstatement is used, the scope of the Format 2PERFORMstatement is all of the statements withinproc-1, all of the statements betweenproc-1andproc-2, and all of the statements inproc-2. - In a
proc-1 THRU proc-2construct,proc-2must followproc-1in the source code. - In a Format 2
PERFORMstatement, theWITH TESTphrase is optional. In the absence of aWITH TESTphrase,WITH TEST BEFOREis assumed. - The
WITH TEST BEFOREphrase indicates that the test ofcondition-1takes place before executing the statements within the scope of thePERFORMstatement. Ifcondition-1testsTRUEinitially, the statements within the scope of thePERFORMstatement are not executed, and control is passed to the next statement after thePERFORMstatement. - The
WITH TEST AFTERphrase indicates that the test ofcondition-1takes place after executing the statements within the scope of thePERFORMstatement. When theWITH TEST AFTERphrase is used, the statements within the scope of thePERFORMstatement will be executed at least one time. - The
VARYINGphrase is used to increment, or decrement a numeric counter variable by a fixed amount. When using theVARYINGphrase,condition-1is set to a test of the numeric counter variable. Condition-1may contain the wordEXIT. This is aPERFORM UNTIL EXITstatement, and the condition tests true when anEXIT PERFORM CYCLEstatement is reached.
Format 3
A Format 3 PERFORM statement executes a statement-list, iterated within the bounds of a PERFORM and END-PERFORM statement, once, or multiple times as designated by the FOREVER phrase or the TIMES phrase.
PERFORM [ { FOREVER } ]
{numeric-1 TIMES }
[ statement-list ]
[ END-PERFORM ]
Syntax:
numeric-nis a numeric literal or data item.statement-listis a list of imperative and/or conditional statements.
General Rules:
- The scope of the Format 3
PERFORMstatement is all of the statements instatement-list, which are entered between thePERFORMandEND-PERFORMstatements. - The
TIMESphrase indicates that the number of times that statements within the scope of thePERFORMstatement are to be executed. After the statements within the scope of thePERFORMstatement have been executed the designated number ofTIMES, control returns to the next statement in the program after thePERFORMstatement. - The
FOREVERphrase indicate s that the statements within the scope of thePERFORMstatement should continue to execute continuously. A program may exit from aPERFORM FOREVERphrase with anEXIT PERFORM CYCLEstatement.
Format 4
A Format 4 PERFORM statement executes a statement-list, iterated within the bounds of a PERFORM and END-PERFORM statement UNTIL a named condition tests true.
PERFORM
[WITH TEST {BEFORE}]
{AFTER }
VARYING { num-1 FROM num-2 by num-3 } ] UNTIL condition-1
[ statement-list ]
[ END-PERFORM ]
Syntax:
num-nis a numeric literal or data item.condition-1is a conditional statement.statement-listis a list of imperative and/or conditional statements.
General Rules:
- The scope of the Format 4
PERFORMstatement is all of the statements instatement-list, which are entered between thePERFORMandEND-PERFORMstatements. Condition-1is the condition-test that determines whether or not to perform the statements within the scope of thePERFORMstatement.Condition-1may contain the wordEXIT. This is aPERFORM UNTIL EXITstatement, and the condition tests true when anEXIT PERFORM CYCLEstatement is reached.- In a Format 4
PERFORMstatement, theWITH TESTphrase is optional. In the absence of aWITH TESTphrase,WITH TEST BEFOREis assumed. - The
WITH TEST BEFOREphrase indicates that the test ofcondition-1takes place before executing the statements within the scope of thePERFORMstatement. Ifcondition-1testsTRUEinitially, the statements within the scope of thePERFORMstatement are not executed, and control is passed to the next statement after thePERFORMstatement. - The
WITH TEST AFTERphrase indicates that the test ofcondition-1takes place after executing the statements within the scope of thePERFORMstatement. When theWITH TEST AFTERphrase is used, the statements within the scope of thePERFORMstatement will be executed at least one time. - The
VARYINGphrase is used to increment, or decrement a numeric counter variable by a fixed amount. When using theVARYINGphrase,condition-1is set to a test of the numeric counter variable.
Code Sample:
IDENTIFICATION DIVISION.
PROGRAM-ID. PERFORM-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 CTR1 PIC 9(4).
77 CTR2 PIC 9(4).
77 CTR3 PIC 9(4).
77 CTR4 PIC 9(4).
77 COUNTER PIC 9(4).
77 DUMMY PIC X.
*
PROCEDURE DIVISION.
MAIN.
PERFORM PARA-1 WITH TEST BEFORE
VARYING CTR1 FROM 1 BY 1 UNTIL CTR1 > 2.
PERFORM PARA-1 THRU PARA-1-END WITH TEST AFTER
VARYING CTR2 FROM 10 BY 10 UNTIL CTR2 > 20.
MOVE 1 TO COUNTER.
PERFORM FOREVER
ADD 1 TO COUNTER
IF COUNTER > 2
EXIT PERFORM
END-IF
END-PERFORM.
MOVE 1 TO COUNTER.
PERFORM 1 TIMES
ADD 1 TO COUNTER
IF COUNTER > 2
EXIT PERFORM
END-IF
END-PERFORM.
MOVE 1 TO COUNTER.
PERFORM WITH TEST BEFORE
VARYING CTR3 FROM 1 BY 1 UNTIL CTR3 > 2
ADD 1 TO COUNTER
IF COUNTER > 2
EXIT PERFORM CYCLE
END-IF
END-PERFORM.
PERFORM WITH TEST AFTER
VARYING CTR4 FROM 1 BY 1 UNTIL CTR4 > 2
ADD 1 TO COUNTER
IF COUNTER > 2
EXIT PERFORM CYCLE
END-IF
END-PERFORM.
PERFORM PARA-1 2 TIMES.
PERFORM PARA-1 THRU PARA-1-END 2 TIMES.
PERFORM PARA-2 THRU PARA-2-END FOREVER.
*
PARA-1.
CONTINUE.
PARA-1-END.
EXIT.
PARA-2.
ADD 1 TO COUNTER.
IF COUNTER >5
DISPLAY "PERFORM-1 FINISHED!" LINE 15 COL 10
ACCEPT DUMMY LINE 15 COL 30
STOP RUN.
PARA-2-END.
EXIT.