The END-COMPUTE statement represents a critical component of structured arithmetic programming in COBOL, serving as an explicit scope terminator that clearly defines the boundaries of COMPUTE statement blocks. This statement embodies modern programming principles by providing unambiguous termination points for complex arithmetic expressions, enabling sophisticated error handling for calculation overflow conditions, and supporting the development of robust, maintainable enterprise applications that require precise control over mathematical computations and their associated error conditions.
The END-COMPUTE statement implements sophisticated scope management for arithmetic expressions. When used with conditional phrases like ON SIZE ERROR and NOT ON SIZE ERROR, END-COMPUTE creates a comprehensive framework for handling both successful calculations and overflow conditions that may arise during complex mathematical operations.
This explicit termination is particularly important in financial applications where precision and error handling are paramount. END-COMPUTE enables developers to implement sophisticated calculation logic while maintaining code clarity and ensuring that arithmetic operations are properly bounded within defined scopes.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647IDENTIFICATION DIVISION. PROGRAM-ID. END-COMPUTE-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. 01 FINANCIAL-CALCULATIONS. 05 PRINCIPAL-AMOUNT PIC 9(9)V99. 05 INTEREST-RATE PIC V9999. 05 TIME-PERIOD PIC 9(3). 05 COMPOUND-INTEREST PIC 9(11)V99. 05 TOTAL-AMOUNT PIC 9(11)V99. 01 CALCULATION-FLAGS. 05 CALCULATION-ERROR-FLAG PIC X VALUE 'N'. 88 CALCULATION-ERROR VALUE 'Y'. 88 CALCULATION-OK VALUE 'N'. PROCEDURE DIVISION. MAIN-PROCESSING. MOVE 10000.00 TO PRINCIPAL-AMOUNT MOVE 0.05 TO INTEREST-RATE MOVE 5 TO TIME-PERIOD *> Complex compound interest calculation with error handling COMPUTE COMPOUND-INTEREST = PRINCIPAL-AMOUNT * ((1 + INTEREST-RATE) ** TIME-PERIOD - 1) ON SIZE ERROR DISPLAY 'Overflow in compound interest calculation' SET CALCULATION-ERROR TO TRUE NOT ON SIZE ERROR DISPLAY 'Compound interest calculated: ' COMPOUND-INTEREST COMPUTE TOTAL-AMOUNT = PRINCIPAL-AMOUNT + COMPOUND-INTEREST ON SIZE ERROR DISPLAY 'Overflow in total amount calculation' SET CALCULATION-ERROR TO TRUE NOT ON SIZE ERROR DISPLAY 'Total amount: ' TOTAL-AMOUNT END-COMPUTE END-COMPUTE IF CALCULATION-ERROR DISPLAY 'Calculation failed due to overflow' ELSE DISPLAY 'All calculations completed successfully' END-IF STOP RUN.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697IDENTIFICATION DIVISION. PROGRAM-ID. ADVANCED-END-COMPUTE. DATA DIVISION. WORKING-STORAGE SECTION. 01 STATISTICAL-CALCULATIONS. 05 DATA-POINTS PIC 9(5). 05 SUM-VALUES PIC 9(11)V99. 05 SUM-SQUARES PIC 9(13)V99. 05 MEAN-VALUE PIC 9(9)V99. 05 VARIANCE PIC 9(11)V99. 05 STANDARD-DEVIATION PIC 9(9)V99. 01 ARRAY-DATA. 05 SAMPLE-SIZE PIC 9(5) VALUE 1000. 05 SAMPLE-VALUES PIC 9(7)V99 OCCURS 1000 TIMES. 01 PROCESSING-CONTROLS. 05 CURRENT-INDEX PIC 9(5). 05 CALCULATION-PHASE PIC 9(2). 05 ERROR-COUNT PIC 9(5). PROCEDURE DIVISION. MAIN-PROCESSING. PERFORM INITIALIZE-DATA PERFORM CALCULATE-STATISTICS PERFORM DISPLAY-RESULTS STOP RUN. CALCULATE-STATISTICS. *> Phase 1: Calculate sum and sum of squares MOVE 1 TO CALCULATION-PHASE PERFORM VARYING CURRENT-INDEX FROM 1 BY 1 UNTIL CURRENT-INDEX > SAMPLE-SIZE COMPUTE SUM-VALUES = SUM-VALUES + SAMPLE-VALUES(CURRENT-INDEX) ON SIZE ERROR DISPLAY 'Overflow in sum calculation at index: ' CURRENT-INDEX ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR COMPUTE SUM-SQUARES = SUM-SQUARES + (SAMPLE-VALUES(CURRENT-INDEX) ** 2) ON SIZE ERROR DISPLAY 'Overflow in sum of squares at index: ' CURRENT-INDEX ADD 1 TO ERROR-COUNT END-COMPUTE END-COMPUTE END-PERFORM *> Phase 2: Calculate mean MOVE 2 TO CALCULATION-PHASE COMPUTE MEAN-VALUE = SUM-VALUES / SAMPLE-SIZE ON SIZE ERROR DISPLAY 'Overflow in mean calculation' ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR DISPLAY 'Mean calculated: ' MEAN-VALUE END-COMPUTE *> Phase 3: Calculate variance and standard deviation MOVE 3 TO CALCULATION-PHASE COMPUTE VARIANCE = (SUM-SQUARES - (SAMPLE-SIZE * (MEAN-VALUE ** 2))) / (SAMPLE-SIZE - 1) ON SIZE ERROR DISPLAY 'Overflow in variance calculation' ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR DISPLAY 'Variance calculated: ' VARIANCE COMPUTE STANDARD-DEVIATION = VARIANCE ** 0.5 ON SIZE ERROR DISPLAY 'Overflow in standard deviation calculation' ADD 1 TO ERROR-COUNT NOT ON SIZE ERROR DISPLAY 'Standard deviation calculated: ' STANDARD-DEVIATION END-COMPUTE END-COMPUTE. INITIALIZE-DATA. MOVE ZEROS TO SUM-VALUES, SUM-SQUARES, ERROR-COUNT *> Initialize sample data with random values PERFORM VARYING CURRENT-INDEX FROM 1 BY 1 UNTIL CURRENT-INDEX > SAMPLE-SIZE COMPUTE SAMPLE-VALUES(CURRENT-INDEX) = FUNCTION RANDOM(CURRENT-INDEX) * 1000 ON SIZE ERROR MOVE 100.00 TO SAMPLE-VALUES(CURRENT-INDEX) END-COMPUTE END-PERFORM. DISPLAY-RESULTS. DISPLAY 'Statistical Analysis Results:' DISPLAY 'Sample Size: ' SAMPLE-SIZE DISPLAY 'Sum of Values: ' SUM-VALUES DISPLAY 'Mean Value: ' MEAN-VALUE DISPLAY 'Variance: ' VARIANCE DISPLAY 'Standard Deviation: ' STANDARD-DEVIATION DISPLAY 'Calculation Errors: ' ERROR-COUNT.
END-COMPUTE is not required for simple COMPUTE statements without conditional phrases. However, it becomes essential when using ON SIZE ERROR or NOT ON SIZE ERROR clauses, in nested structures, or when you want to explicitly define the scope of arithmetic operations for better code clarity.
END-COMPUTE works with ON SIZE ERROR and NOT ON SIZE ERROR phrases to provide comprehensive handling of arithmetic overflow conditions. When overflow occurs during computation, the ON SIZE ERROR clause is executed, allowing for error logging, recovery actions, or alternative calculations.
Yes, END-COMPUTE is particularly valuable with complex mathematical expressions involving multiple operations, functions, and nested calculations. It provides explicit scope boundaries that make complex arithmetic logic more readable and maintainable while enabling comprehensive error handling.