COBOL Perform Varying

COBOL Perform Until COBOL Files

Advertisements


PERFORM VARYING is mainly used to execute the set/block of statements or paragraph /section repetitively until the specified condition in the perform satisfied.

The literal used in the condition initialized or incremented in the PERFORM itself.

PERFORM VARYING was INLINED and OUTLINED.

It is mostly similar to PERFORM UNTIL but no initialization or incremented required separately for the literal used in condition.


Syntax:



Below are the syntax for various types of perform varying.


Inline PerformOutline Perform
PERFORM VARYING:

PERFORM VARYING {index/counter}
       FROM  {start value}  
             BY {step value}
       UNTIL Condition
       Statements
END-PERFORM.
PERFORM VARYING:

PERFORM {Paragraph/Section}      
      [{THROGH/THRU} {Paragraph/Section}      
      VARYING {index/counter}
       FROM  {start value}  
             BY {step value}
       UNTIL Condition.
WITH AFTER:

PERFORM VARYING {index/counter}
       FROM  {start value}  
             BY {step value}
       UNTIL Condition
     [AFTER  {start value}  
             BY {step value}
       UNTIL Condition]
       Statements
END-PERFORM.	
WITH AFTER:

PERFORM {Paragraph/Section}      
      [{THROGH/THRU} {Paragraph/Section}      
      VARYING {index/counter}
       FROM  {start value}  
             BY {step value}
       UNTIL Condition
     [AFTER  {start value}  
             BY {step value}
       UNTIL Condition].	
WITH TEST BEFORE:

PERFORM  WITH TEST BEFORE
VARYING {index/counter}
       FROM  {start value}  
             BY {step value}
       UNTIL Condition
     [AFTER  {start value}  
             BY {step value}
       UNTIL Condition]
       Statements
END-PERFORM.
WITH TEST BEFORE:

PERFORM {Paragraph/Section}      
      [{THROGH/THRU} {Paragraph/Section}  
      WITH TEST BEFORE    
VARYING {index/counter}
       FROM  {start value}  
             BY {step value}
       UNTIL Condition
     [AFTER  {start value}  
             BY {step value}
       UNTIL Condition].
	
WITH TEST AFTER:

PERFORM  WITH TEST AFTER
VARYING {index/counter}
       FROM  {start value}  
             BY {step value}
       UNTIL Condition
     [AFTER  {start value}  
             BY {step value}
       UNTIL Condition]
       Statements
END-PERFORM.
	
WITH TEST AFTER:

PERFORM {Paragraph/Section}      
      [{THROGH/THRU} {Paragraph/Section}  
      WITH TEST AFTER    
VARYING {index/counter}
       FROM  {start value}  
             BY {step value}
       UNTIL Condition
     [AFTER  {start value}  
             BY {step value}
       UNTIL Condition].		

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 will go to the next statements to be executed.

WITH TEST BEFORE is default with Perform.


WITH TEST AFTER:

WITH TEST AFTER is used to test the condition after executing the statements block. It will simply execute the statements block without checking the condition, after the iteration completed, it will check for the condition.

i.e. This will execute the statements block once time even though the condition is false for first time too.

WITH TEST AFTER is not default with Perform and used in special cases like the code needs to be executed at least once in any case.


AFTER:

In PERFORM VARYING, the AFTER will be act as inline PERFORM VARYING.

i.e. for one iteration of PERFORM VARYING in the FROM condition, the AFTER will complete its all iterations up to the UNTIL condition satisfied in AFTER and then control will go to the next iteration of FROM Condition.

Indirectly it will act as NESTED PERFORM, if they perform has AFTER coded in it.


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.                                                        



COBOL Perform Until COBOL Files

Advertisements