Summary -

PERFORM VARYING used to execute the statements-block/procedures by increasing or decreasing the values of one or more identifiers for the iterations. The literal used in the condition initialized or incremented in the PERFORM itself.

PERFORM VARYING is both in-line and out-of-line. It is similar to PERFORM UNTIL, but no initialization or increment required to specified separately for the literal used in condition.

The VARYING phrase increases or decreases the value of one or more identifiers or index-names according to certain rules.

Syntax -

PERFORM VARYING Syntax

If procedure-name-1 is specified, imperative-statement-1 and the END-PERFORM phrase must not be specified. If procedure-name-1 is omitted, imperative-statement-1 and the END-PERFORM phrase must be specified. If procedure-name-1 is omitted, the AFTER phrase must not be specified.

identifier-2 through identifier-7 -

Must name a numeric elementary item. These identifiers can't be date fields.

literal-1 through literal-4 -

Must represent a numeric literal.

condition-1, condition-2 -

Can be any logical expressions with a combination of operands and operators. The loop iterated until the condition is false and once the condition is true, the loops gets terminated and control is passed to the next executable statement following the PERFORM statement.

The operands in the condition-1, condition-2 can be subscripted, reference modified or is a function-identifier, the subscript, reference-modifier or function is evaluated each time the condition is tested.

WITH TEST BEFORE -

WITH TEST BEFORE is used to test the condition before executing the statements block. If the condition is true, then simply terminate the loop and control transfers to the next executable statements. WITH TEST BEFORE is default phase with PERFORM if not specified any.

WITH TEST AFTER -

WITH TEST AFTER is used to test the condition after executing the statements block. It simply executes the statements block without checking the condition.

After one iteration completed, it validates the condition. i.e. The statements-block gets executed one time at least even though the condition is true for first time itself.

AFTER -

In PERFORM VARYING, the AFTER acts as inline PERFORM VARYING. That is, for one iteration of PERFORM VARYING with the FROM condition, the AFTER phrase completes its all iterations the UNTIL condition satisfied. Once the AFTER phrase completes all iterations, then the control transfers to the next iteration of FROM Condition.

Practical Example -

Inline Perform Varying - Code

IDENTIFICATION DIVISION.                                         
PROGRAM-ID. PERVARI.                                            
ENVIRONMENT DIVISION.                                            
DATA DIVISION.                                                   
WORKING-STORAGE SECTION.                                         
01 STD-MARKS                 PIC 9(03).                          
01 I                         PIC 9(01).                          
PROCEDURE DIVISION.                                             
    PERFORM                                                      
    VARYING I FROM 1 BY 1                                       
      UNTIL I > 3.                                               
         ACCEPT STD-MARKS.                                       
         DISPLAY 'STUDENT ' I ' DETAILS '                        
         EVALUATE STD-MARKS                                      
            WHEN 60 THRU 100                                     
                 DISPLAY 'STUDENT GOT FIRST CLASS   '            
            WHEN 50 THRU 59                                      
                 DISPLAY 'STUDENT GOT SECOND CLASS  '            
            WHEN 35 THRU 49                                      
                 DISPLAY 'STUDENT GOT THIRD CLASS   '            
            WHEN OTHER                                           
                 DISPLAY 'STUDENT FAILED            '            
         END-EVALUATE                                            
    END-PERFORM.                                                 
    STOP RUN.                                                    

Outline Perform Varying - Code

IDENTIFICATION DIVISION.                                         
PROGRAM-ID. PERVAROT.                                           
ENVIRONMENT DIVISION.                                            
DATA DIVISION.                                                  
WORKING-STORAGE SECTION.                                        
01 STD-MARKS                 PIC 9(03).                          
01 I                         PIC 9(01).                          
PROCEDURE DIVISION.                                              
    PERFORM DISP-CLASS                                           
       THRU DISP-CLASS-EXIT                                     
    VARYING I FROM 1 BY 1                                        
      UNTIL I > 3.                                               
    STOP RUN.                                                   
DISP-CLASS.                                                      
    ACCEPT STD-MARKS.                                            
    DISPLAY 'STUDENT ' I ' DETAILS '.                           
    EVALUATE STD-MARKS                                           
       WHEN 60 THRU 100                                         
            DISPLAY 'STUDENT GOT FIRST CLASS   '                
       WHEN 50 THRU 59                                          
            DISPLAY 'STUDENT GOT SECOND CLASS  '                 
       WHEN 35 THRU 49                                          
            DISPLAY 'STUDENT GOT THIRD CLASS   '                
       WHEN OTHER                                                
            DISPLAY 'STUDENT FAILED            '                 
    END-EVALUATE.                                                
DISP-CLASS-EXIT.                                                 
    EXIT.