Skip to content

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:

  1. proc-n is a procedure or section declared in the program.
  2. numeric-n is a numeric literal or data item.
  3. num-n is a numeric literal or data item.

General Rules:

  1. THRU and THROUGH are synonyms
  2. When the THRU statement is not used, the scope of the Format 1 PERFORM statement is all of the statements within proc-1.
  3. When the THRU statement is used, the scope of the Format 1 PERFORM statement is all of the statements within proc-1, all of the statements between proc-1 and proc-2, and all of the statements in proc-2.
  4. In a proc-1 thru proc-2 construct, proc-2 must follow proc-1 in the source code.
  5. The TIMES phrase indicates that the number of times that statements within the scope of the PERFORM statement are to be executed. After the statements within the scope of the PERFORM statement have been executed the designated number of TIMES, control returns to the next statement in the program after the PERFORM statement.
  6. The FOREVER phrase indicates that the statements within the scope of the PERFORM statement should continue to execute continuously. A program may exit from a PERFORM FOREVER phrase with an EXIT 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:

  1. proc-n is a procedure or section declared in the program.
  2. num-n is a numeric literal or data item.
  3. condition-1 is a conditional statement.

General Rules:

  1. THRU and THROUGH are synonyms.
  2. When the THRU statement is not used, the scope of the Format 2 PERFORM statement is all of the statements within proc-1.
  3. When the THRU statement is used, the scope of the Format 2 PERFORM statement is all of the statements within proc-1, all of the statements between proc-1 and proc-2, and all of the statements in proc-2.
  4. In a proc-1 THRU proc-2 construct, proc-2 must follow proc-1 in the source code.
  5. In a Format 2 PERFORM statement, the WITH TEST phrase is optional. In the absence of a WITH TEST phrase, WITH TEST BEFORE is assumed.
  6. The WITH TEST BEFORE phrase indicates that the test of condition-1 takes place before executing the statements within the scope of the PERFORM statement. If condition-1 tests TRUE initially, the statements within the scope of the PERFORM statement are not executed, and control is passed to the next statement after the PERFORM statement.
  7. The WITH TEST AFTER phrase indicates that the test of condition-1 takes place after executing the statements within the scope of the PERFORM statement. When the WITH TEST AFTER phrase is used, the statements within the scope of the PERFORM statement will be executed at least one time.
  8. The VARYING phrase is used to increment, or decrement a numeric counter variable by a fixed amount. When using the VARYING phrase, condition-1 is set to a test of the numeric counter variable.
  9. Condition-1 may contain the word EXIT. This is a PERFORM UNTIL EXIT statement, and the condition tests true when an EXIT 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:

  1. numeric-n is a numeric literal or data item.
  2. statement-list is a list of imperative and/or conditional statements.

General Rules:

  1. The scope of the Format 3 PERFORM statement is all of the statements in statement-list, which are entered between the PERFORM and END-PERFORM statements.
  2. The TIMES phrase indicates that the number of times that statements within the scope of the PERFORM statement are to be executed. After the statements within the scope of the PERFORM statement have been executed the designated number of TIMES, control returns to the next statement in the program after the PERFORM statement.
  3. The FOREVER phrase indicate s that the statements within the scope of the PERFORM statement should continue to execute continuously. A program may exit from a PERFORM FOREVER phrase with an EXIT 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:

  1. num-n is a numeric literal or data item.
  2. condition-1 is a conditional statement.
  3. statement-list is a list of imperative and/or conditional statements.

General Rules:

  1. The scope of the Format 4 PERFORM statement is all of the statements in statement-list, which are entered between the PERFORM and END-PERFORM statements.
  2. Condition-1 is the condition-test that determines whether or not to perform the statements within the scope of the PERFORM statement.
  3. Condition-1 may contain the word EXIT. This is a PERFORM UNTIL EXIT statement, and the condition tests true when an EXIT PERFORM CYCLE statement is reached.
  4. In a Format 4 PERFORM statement, the WITH TEST phrase is optional. In the absence of a WITH TEST phrase, WITH TEST BEFORE is assumed.
  5. The WITH TEST BEFORE phrase indicates that the test of condition-1 takes place before executing the statements within the scope of the PERFORM statement. If condition-1 tests TRUE initially, the statements within the scope of the PERFORM statement are not executed, and control is passed to the next statement after the PERFORM statement.
  6. The WITH TEST AFTER phrase indicates that the test of condition-1 takes place after executing the statements within the scope of the PERFORM statement. When the WITH TEST AFTER phrase is used, the statements within the scope of the PERFORM statement will be executed at least one time.
  7. The VARYING phrase is used to increment, or decrement a numeric counter variable by a fixed amount. When using the VARYING 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.
Back to top