PERFORM Statement


PERFORM statement executes the statement block repeatedly. The repetitive execution can be conditional or unconditional. If any condition is coded with it, the statements block gets executed until the coded condition is true.

END-PERFORM -

END-PERFORM is used to end the scope of the in-line PERFORM statement. It is optional if the statements under PERFORM end with a period. The period is considered as a logical end of the PERFORM statement.

PERFORM Types -


PERFORM statement is mainly two types at high level based on how it is coded to perform the task –

  • Inline PERFORM
  • Outline PERFORM

The below table describes the differences between inline an outline PERFORM statements –

Inline PERFORMOutline PERFORM
PERFORM 
   statements-block
END-PERFORM.
PERFORM paragraph-name|section-name.
The PERFORM statement executes the statements block that is coded inline (between PERFORM and END-PERFORM) The PERFORM statement executes statements block that are coded outline (in a separate paragraph or section outside of PERFORM).
This requires no seperate paragraph or section. It requires a separate paragraph or seection that needs to be coded with statements block.
Not required to transfer the control to paragraph or section. Control transfers to the paragraphs or sections
Scope terminator END-PERFORM is mandatory Scope terminator END-PERFORM is not required
Note!
  • A PERFORM statement should not call itself and it can produce unpredictable results.
  • The inline and outline PERFORM formats can't be coded together.

Different PERFORM Formats -


PERFORM statement has five different formats to complete the tasks and those are -

  • Simple PERFORM
  • PERFORM...THRU or PERFORM...THROUGH
  • PERFORM...TIMES
  • PERFORM...UNTIL
  • PERFORM...VARYING

Simple PERFORM


Simple PERFORM is a way to execute a given paragraph or section once, and control is passed to the next statement in the flow. It is both inline and outline.

Inline PERFORMOutline PERFORM
PERFORM 
   statements-block
END-PERFORM.
PERFORM paragraph-name|section-name
  • paragraph-name - The name of the paragraph we want to execute.
  • section-name - The name of the section we want to execute.

Example - Simple Outline PERFORM

----+----1----+----2----+----3----+----4----+----5----+
       ...
       PROCEDURE DIVISION.
       MAIN-PARA.
           DISPLAY 'Start of Main Paragraph'.
           PERFORM 1000-DISPLAY1
           DISPLAY ''. 
           STOP RUN.
       ...
       1000-DISPLAY1.
           DISPLAY 'Inside DISPLAY1 Paragraph'.

OutputEnd of Main Paragraph -

Start of Main Paragraph
Inside DISPLAY1 Paragraph
End of Main Paragraph

PERFORM...THRU


PERFORM statement is used to execute a specific paragraph or a range of paragraphs. PERFORM...THROUGH (or PERFORM...THRU) specifies a block of consecutive paragraphs to be executed. It is outline PERFORM.

PERFORM paragraph-1 THROUGH|THRU paragraph-2

Example - Executing paragraphs using PERFORM...THRU

----+----1----+----2----+----3----+----4----+----5----+
       ...
       PROCEDURE DIVISION.
           PERFORM 1000-DISPLAY1
              THRU 3000-DISPLAY3.
           STOP RUN.

       1000-DISPLAY1.
           DISPLAY "Paragraph1".
       2000-DISPLAY2.
           DISPLAY "Paragraph2".
       3000-DISPLAY3.
           DISPLAY "Paragraph3".
       4000-DISPLAY3.
           DISPLAY "Paragraph4".

Output -

Paragraph1
Paragraph2
Paragraph3

PERFORM...TIMES


PERFORM...TIMES statement is used to execute a specific paragraph or section a certain number of times. It is both inline and outline.

Inline PERFORM...TIMES Outline PERFORM...TIMES
PERFORM integer-1 TIMES
    Statements-block
END-PERFORM.
PERFORM paragraph-1
   [THRU paragraph-2] 
   integer-1 TIMES.

Example - Displaying loop iterations using inline PERFORM..TIMES.

----+----1----+----2----+----3----+----4----+----5----+
       ...
       PROCEDURE DIVISION.
	       DISPLAY "Before Inline PERFORM...TIMES".
           PERFORM 6 TIMES
              DISPLAY "Iteration: " WS-I 
              COMPUTE WS-I = WS-I + 1
           END-PERFORM.
	       DISPLAY "After Inline PERFORM...TIMES".
           ...

Output -

Before Inline PERFORM...TIMES
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
After Inline PERFORM...TIMES

PERFORM...UNTIL


PERFORM...UNTIL statement is used to execute a statement block repetitively until a certain condition is true. The variable used in condition initialization, increment or decrement should be taken care separately. It is both inline and outline.

Inline PEFORM...UNTILOutline PEFORM...UNTIL
PERFORM 
  [WITH TEST BEFORE|
  WITH TEST AFTER]
  UNTIL condition-1
	statements-block
END-PERFORM.
PERFORM paragraph-1 
  [THRU paragraph-2]
  [WITH TEST BEFORE|
  WITH TEST AFTER]
  UNTIL condition-1.
  • condition - The condition that determines when the loop should terminate. Once the condition becomes true, the loop gets terminated.
  • WITH TEST BEFORE - tests the condition before executing the statements block. It is default with PERFORM if not coded any.
  • WITH TEST AFTER - tests the condition after executing the statements block.

Example - Displaying loop iterations using inline PERFORM...UNTIL.

----+----1----+----2----+----3----+----4----+----5----+
       ...
       PROCEDURE DIVISION.
           DISPLAY "Before PERFORM UNTIL".
           PERFORM UNTIL WS-I > 3 
                   DISPLAY "WS-I: " WS-I
                   COMPUTE WS-I = WS-I + 1
           END-PERFORM.
           DISPLAY "After PERFORM UNTIL".
           ...

Output -

Before PERFORM UNTIL
WS-I: 1             
WS-I: 2             
WS-I: 3             
After PERFORM UNTIL 

PERFORM...VARYING


The PERFORM...VARYING is used to execute a specific paragraph or section repetitively while varying the value of one or more control variables. It is both inline and outline. It is similar to PERFORM...UNTIL, but no initialization, increment, or decrement is required to code separately for the variable used in the condition.

Inline PEFORM...VARYINGOutline PEFORM...VARYING
PERFORM 
  [WITH TEST BEFORE|
  WITH TEST AFTER]
VARYING control-variable-1 
   FROM start-value-1 
     BY increment-value-1
  UNTIL condition-1
END-PERFORM.
PERFORM paragraph-1 
  [THRU paragraph-2]
  [WITH TEST BEFORE|
  WITH TEST AFTER]
VARYING control-variable-1 
   FROM start-value-1 
     BY increment-value-1
  UNTIL condition-1.

Example - Displaying loop iterations using inline PERFORM...VARYING.

----+----1----+----2----+----3----+----4----+----5----+
       ...
       PROCEDURE DIVISION.
           DISPLAY "Before Inline PERFORM...VARYING".
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 2
              DISPLAY "Iteration: " WS-I
           END-PERFORM.
           DISPLAY "After Inline PERFORM...VARYING".
           STOP RUN.

Output -

Before Inline PERFORM...VARYING
Iteration: 1
Iteration: 2
After Inline PERFORM...VARYING