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-n
is a procedure or section declared in the program.numeric-n
is a numeric literal or data item.num-n
is a numeric literal or data item.
General Rules:
THRU
andTHROUGH
are synonyms- When the
THRU
statement is not used, the scope of the Format 1PERFORM
statement is all of the statements withinproc-1
. - When the
THRU
statement is used, the scope of the Format 1PERFORM
statement is all of the statements withinproc-1
, all of the statements betweenproc-1
andproc-2
, and all of the statements inproc-2
. - In a
proc-1
thruproc-2
construct,proc-2
must followproc-1
in the source code. - The
TIMES
phrase indicates that the number of times that statements within the scope of thePERFORM
statement are to be executed. After the statements within the scope of thePERFORM
statement have been executed the designated number ofTIMES
, control returns to the next statement in the program after thePERFORM
statement. - The
FOREVER
phrase indicates that the statements within the scope of thePERFORM
statement should continue to execute continuously. A program may exit from aPERFORM FOREVER
phrase with anEXIT PERFORM CYCLE
statement.
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-n
is a procedure or section declared in the program.num-n
is a numeric literal or data item.condition-1
is a conditional statement.
General Rules:
THRU
andTHROUGH
are synonyms.- When the
THRU
statement is not used, the scope of the Format 2PERFORM
statement is all of the statements withinproc-1
. - When the
THRU
statement is used, the scope of the Format 2PERFORM
statement is all of the statements withinproc-1
, all of the statements betweenproc-1
andproc-2
, and all of the statements inproc-2
. - In a
proc-1 THRU proc-2
construct,proc-2
must followproc-1
in the source code. - In a Format 2
PERFORM
statement, theWITH TEST
phrase is optional. In the absence of aWITH TEST
phrase,WITH TEST BEFORE
is assumed. - The
WITH TEST BEFORE
phrase indicates that the test ofcondition-1
takes place before executing the statements within the scope of thePERFORM
statement. Ifcondition-1
testsTRUE
initially, the statements within the scope of thePERFORM
statement are not executed, and control is passed to the next statement after thePERFORM
statement. - The
WITH TEST AFTER
phrase indicates that the test ofcondition-1
takes place after executing the statements within the scope of thePERFORM
statement. When theWITH TEST AFTER
phrase is used, the statements within the scope of thePERFORM
statement will be executed at least one time. - The
VARYING
phrase is used to increment, or decrement a numeric counter variable by a fixed amount. When using theVARYING
phrase,condition-1
is set to a test of the numeric counter variable. Condition-1
may contain the wordEXIT
. This is aPERFORM UNTIL EXIT
statement, and the condition tests true when anEXIT PERFORM CYCLE
statement 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-n
is a numeric literal or data item.statement-list
is a list of imperative and/or conditional statements.
General Rules:
- The scope of the Format 3
PERFORM
statement is all of the statements instatement-list
, which are entered between thePERFORM
andEND-PERFORM
statements. - The
TIMES
phrase indicates that the number of times that statements within the scope of thePERFORM
statement are to be executed. After the statements within the scope of thePERFORM
statement have been executed the designated number ofTIMES
, control returns to the next statement in the program after thePERFORM
statement. - The
FOREVER
phrase indicate s that the statements within the scope of thePERFORM
statement should continue to execute continuously. A program may exit from aPERFORM FOREVER
phrase with anEXIT PERFORM CYCLE
statement.
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-n
is a numeric literal or data item.condition-1
is a conditional statement.statement-list
is a list of imperative and/or conditional statements.
General Rules:
- The scope of the Format 4
PERFORM
statement is all of the statements instatement-list
, which are entered between thePERFORM
andEND-PERFORM
statements. Condition-1
is the condition-test that determines whether or not to perform the statements within the scope of thePERFORM
statement.Condition-1
may contain the wordEXIT
. This is aPERFORM UNTIL EXIT
statement, and the condition tests true when anEXIT PERFORM CYCLE
statement is reached.- In a Format 4
PERFORM
statement, theWITH TEST
phrase is optional. In the absence of aWITH TEST
phrase,WITH TEST BEFORE
is assumed. - The
WITH TEST BEFORE
phrase indicates that the test ofcondition-1
takes place before executing the statements within the scope of thePERFORM
statement. Ifcondition-1
testsTRUE
initially, the statements within the scope of thePERFORM
statement are not executed, and control is passed to the next statement after thePERFORM
statement. - The
WITH TEST AFTER
phrase indicates that the test ofcondition-1
takes place after executing the statements within the scope of thePERFORM
statement. When theWITH TEST AFTER
phrase is used, the statements within the scope of thePERFORM
statement will be executed at least one time. - The
VARYING
phrase is used to increment, or decrement a numeric counter variable by a fixed amount. When using theVARYING
phrase,condition-1
is 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.