Inline Perform Varying Example


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

Code -

----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION. 
       PROGRAM-ID. PERFIVAR.
       AUTHOR. MTH.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-I      PIC 9(01).
        
       PROCEDURE DIVISION. 
           DISPLAY "Before PERFORM VARYING".
           PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 6
                   DISPLAY "WS-I: " WS-I
           END-PERFORM.
           DISPLAY "After PERFORM VARYING".

           STOP RUN.

Output -

Before PERFORM VARYING
WS-I: 1
WS-I: 2
WS-I: 3
WS-I: 4
WS-I: 5
WS-I: 6
After PERFORM VARYING 

Explaining Example -

In the above example:

  • It is a inline PEFORM VARYING loop that iterates the loop until WS-I greater than 6.
  • It displays the iteration number, increment will be done by PERFORM and no special increment is required.