PERFORM VARYING


  • PERFORM...VARYING executes a specific paragraph or section repetitively while varying the values of the variables until the condition is satisfied.
  • PERFORM..VARYINNG is similar to PERFORM UNTIL, but it handles the variable's initialization, increment, or decrement.
  • This structure provides a "for loop" functionality found in many other programming languages.
  • It is both inline and outline.

Syntax -

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
 [AFTER control-variable-2 
   FROM start-value-2 
     BY increment-value-2
  UNTIL condition-2]
	statements-block
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
 [AFTER control-variable-2 
   FROM start-value-2 
     BY increment-value-2
  UNTIL condition-2].
Note! All statements coded in [ ] are optional. Either WITH TEST BEFORE or WITH TEST AFTER should be coded with PERFORM.

Parameters -

  • paragraph-1 - The starting paragraph to be executed.
  • THRU paragraph-2 - (Optional) If coded, indicates the end of a range of paragraphs to be executed. After executing this ending paragraph, control returns to the start of the range until the coded condition is true.
  • 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.
  • VARYING control-variable-1, control-variable-2 - This specifies the control variable that will be changed with each iteration.
  • FROM start-value-1, start-value-2 - This sets the initial value for the control variable.
  • BY increment-value-1, increment-value-2 - The value by which the control variable will be incremented (or decremented) during each loop iteration.
  • UNTIL condition-1, condition-2 - The condition upon which the loop should terminate. The loop will continue as long as this condition remains false. Once true, the loop terminates.
  • AFTER - Used for nested loops. It indicates that the inner loop should be performed after each iteration of the outer loop. AFTER phrase is only applicable to outline PERFORM...VARYING.

Practical Examples -


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

Code -

----+----1----+----2----+----3----+----4----+
       ...
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-I         PIC 9(01).

       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".
		   ...

OUTPUT -

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

Scenario2 - Displaying loop iterations using inline PERFORM...VARYING...AFTER.

Code -

----+----1----+----2----+----3----+----4----+
       ...
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
          05 WS-I      PIC 9(01). 
          05 WS-J      PIC 9(01).

       PROCEDURE DIVISION. 
           DISPLAY "Before PERFORM...VARYING...AFTER". 
           PERFORM 1000-CALCULATION 
		      THRU 1000-EXIT 
                   VARYING WS-I FROM 1 BY 1 UNTIL WS-I > 2 
                     AFTER WS-J FROM 1 BY 1 UNTIL WS-J > 6.
           DISPLAY "After PERFORM...VARYING...AFTER"

           STOP RUN.
       1000-CALCULATION.
           DISPLAY "WS-I: " WS-I " , WS-J: " WS-J.
       1000-EXIT.
            EXIT.

OUTPUT -

Before PERFORM...VARYING...AFTER
WS-I: 1 , WS-J: 1
WS-I: 1 , WS-J: 2
WS-I: 1 , WS-J: 3
WS-I: 1 , WS-J: 4
WS-I: 1 , WS-J: 5
WS-I: 1 , WS-J: 6
WS-I: 2 , WS-J: 1
WS-I: 2 , WS-J: 2
WS-I: 2 , WS-J: 3
WS-I: 2 , WS-J: 4
WS-I: 2 , WS-J: 5
WS-I: 2 , WS-J: 6
After PERFORM...VARYING...AFTER