Summary -

In this topic, we described about the below sections -

COMPUTE used to calculate the result of arithmetic expressions and assigns the output to one or more data items. If arithmetic statements like ADD, SUBTRACT, MULTPLY and DIVIDE used, the output is from one arithmetic operation.

With the COMPUTE statement, arithmetic operations can be combined without the restrictions. The arithmetic operators used in COMPUTE statement are + (ADD), - (SUBTRACT), * (MULTIPLY), / (DIVIDE) and ** (EXPONENT) statements.

The COMPUTE statement can be more efficient than the separate arithmetic statements written in a series. COMPUTE statement can be used to combine more than one arithmetic operation in a single expression where other arithmetic operation can’t.

Syntax -

COMPUTE Syntax

In the above syntax, ROUNDED, ON SIZE ERROR and NOT ON SIZE ERROR are optional.

identifier-1

Is an elementary numeric item or an elementary numeric-edited item. Can name an elementary floating-point data item.

arithmetic-expression

Can be any arithmetic expression. When the COMPUTE statement is executed, the value of arithmetic expression is calculated and stored the value in identifier-1.

ROUNDED phrase

SIZE ERROR phrase

END-COMPUTE phrase

The END-COMPUTE used to end the scope of the COMPUTE statement. END-COMPUTE is not required when COMPUTE statement ended with period.

Practicle Example -


 IDENTIFICATION DIVISION.                                         
 PROGRAM-ID. PERFTHRU.                                            
 ENVIRONMENT DIVISION.                                            
 DATA DIVISION.                                                   
 WORKING-STORAGE SECTION.                                         
 01 STD-MARKS                 PIC 9(03).                          
 01 I                         PIC 9(01).                          
 PROCEDURE DIVISION.                                              
     MOVE 1         TO I.                                         
     PERFORM DISP-CLASS                                           
        THRU DISP-CLASS-EXIT                                      
        WITH TEST BEFORE                                          
       UNTIL I > 3.                                               
     STOP RUN.                                                    
 DISP-CLASS.                                                      
     ACCEPT STD-MARKS.                                            
     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.                                                
     COMPUTE I = I + 1.                                           
 DISP-CLASS-EXIT.                                                 
     EXIT.