COBOL Compute Statement

COBOL Call Statement COBOL Display Statement

Advertisements


COMPUTE used to calculate the result of arithmetic expressions and stores the same in another variable/data-item.

COMPUTE can use the below arithmetic expressions.


ADD			-		+
SUBTRACT		-		-
MULTIPLY		-		*
DIVIDE			-		/
EXPONENT		-		**

COMPUTE is very simple and efficient when compared to the above operations.

COMPUTE statement can be used to write more than one arithmetic statement in a single expression where other arithmetic operation can’t be.

END-COMPUTE is optional with COMPUTE.

END-COMPUTE will not be required when COMPUTE statement ended with period.


Syntax:

COMPUTE identifier-1 [ROUNDED] =
	Identifier-2  identifier-3	
	[ON SIZE ERROR ]
	[NOT ON SIZE ERROR ]
END-COMPUTE.

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


ROUNDED:

ROUNDED option used to round the fraction result of the compute statement exceeds the length of the target data item fractional places.

If the result of the COMPUTE statement exceeds the length of the target data item fractional places, those will not be rounded until or unless the ROUNDED option specifies. The result will be truncated.

If the ROUNDED option specifies, the last digit of fraction place will be increased by 1 if the next fraction value greater than 5.


SIZE ERROR:

SIZE ERROR is optional in COMPUTE statement.

SIZE ERROR mostly occurs for the cases like


  1. When the result of arithmetic expression exceeds the length of declaration for target item.
  2. When divided by zero occurs.
  3. When exponential errors occurred.

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.                                                        


COBOL Call Statement COBOL Display Statement

Advertisements