160 likes | 169 Views
Learn about the COMPUTE instruction for simple arithmetic, using ADD, MULTIPLY, DIVIDE, SUBTRACT operations. Improve code efficiency with programming tips.
E N D
Table 5.5 The COMPUTE Instruction DATA NAME A B C COMMENTS Value before execution 2 3 10 Initial Values Value after execution of: COMPUTE C = A + B. 2 3 5 Simple addition COMPUTE C = A + B * 2. 2 3 8 Multiplication before addition COMPUTE C = (A + B) * 2. 2 3 10 Parenthesis evaluated first COMPUTE C = A ** B. 2 3 8 algebraically, c = ab COMPUTE C = B ** A. 2 3 9 algebraically, c = ba
Table 5.6 The COMPUTE Instruction (continued) ALGEBRAIC EXPRESSION COBOL COMPUTE x = a + b COMPUTE X = A + B. x = a + b COMPUTE X = (A + B) / 2. 2 x = (a + b)c COMPUTE X = (A + B) * C / 2. 2 x = a + b COMPUTE X = (A + B) / (2 * C). 2c x = a COMPUTE X = A ** .5. x = a + b COMPUTE X = (A ** 2 + B ** 2) / C ** 2 c2
Table 5.7 The ADD Instruction DATA NAME A B C Value before execution 5 10 30 Value after execution of: ADD A TO C 5 10 35 ADD A B TO C 5 10 45 ADD A TO B GIVING C 5 10 15 ADD A 18 B GIVING C 5 10 33 ADD A 18 B TO C 5 10 63 ADD 1 TO B C 5 11 31
Table 5.10 The DIVIDE Instruction DATA NAME A B C Value before execution 5 10 30 Value after execution of: DIVIDE 2 INTO B. 5 5 30 DIVIDE 2 INTO B GIVING C. 5 10 5 DIVIDE B BY 5 GIVING A 2 10 30 DIVIDE A INTO B C 5 2 6 DIVIDE A INTO B GIVING C 5 10 2 DIVIDE 3 INTO A GIVING B REMAINDER C 5 1 2
Table 5.8 The SUBTRACT Instruction DATA NAME A B C D Value before execution 5 10 30 100 Value after execution of: SUBTRACT A FROM C 5 10 25 100 SUBTRACT A B FROM C 5 10 15 100 SUBTRACT A B FROM C GIVING D 5 10 30 15 SUBTRACT 10 FROM C D 5 10 20 90
Table 5.9 The MULTIPLY Instruction DATA NAME A B C Value before execution 5 10 30 Value after execution of: MULTIPLY B BY A GIVING C 5 10 50 MULTIPLY A BY B GIVING C 5 10 50 MULTIPLY A BY B 5 50 15 MULTIPLY B BY A 50 10 30 MULTIPLY A BY 3 GIVING B C 5 15 15
Poor Code MULTIPLY B BY B GIVING B-SQUARED.MULTIPLY 4 BY A GIVING FOUR-A.MULTIPLY FOUR-A BY C GIVING FOUR-A-C.SUBTRACT FOUR-A-C FROM B-SQUARED GIVING RESULT-1.COMPUTE RESULT-2 = RESULT-1 ** .5.SUBTRACT B FROM RESULT-2 GIVING NUMERATOR.MULTIPLY 2 BY A GIVING DENOMINATOR.DIVIDE NUMERATOR BY DENOMINATOR GIVING X. COMPUTE X = (-B + (B ** 2 - ( 4 * A * C)) ** .5) / ( 2 * A). Both sets of code apply to the quadratic formula X = -B + B-4AC 2A Improved Code PROGRAMMING TIP Use the COMPUTE Statement for Multiple Arithmetic Operations
Table 5.11 Arithmetic on Fields with Assumed Decimal Points DATA NAME A B C PICTURE 99 99V9 99V99 Value before execution 12 345 4712 Value after execution of: ADD B TO A 46 345 4712 ADD A TO B 12 465 4712 ADD B TO C 12 345 8162 ADD C TO B 12 816 4712 ADD C TO A 59 345 4712 ADD A TO C 12 465 5912
Table 5.4 The ROUNDED Clause DATA NAME A B C PICTURE 9V99 9V99 9V9 Value before execution 123 456 (immaterial) Value after execution of: ADD A B GIVING C 123 456 57 ADD A B GIVING C ROUNDED 123 456 58
The INITIALIZE statement • Sets elementary or group data names to ‘initial’ value • DOES NOT RESET VALUE TO THAT OF THE VALUE STATEMENT!!! • EX: 01 GROUP-ITEM. 05 NUMERIC-FIELD-1 PIC 9(4). 05 NUMERIC-FIELD-2 PIC 9(4). 05 ALPHANUMERIC-FIELD-1 PIC X(15). 05 ALPHANUMERIC-FIELD-2 PIC X(20). • The statement INITIALIZE GROUP-ITEM • is equivalent to the combined statements: MOVE ZEROS TO NUMERIC-FIELD-1. MOVE ZEROS TO NUMERIC-FIELD-2. MOVE SPACES TO ALPHANUMERIC-FIELD-1. MOVE SPACES TO ALPHANUMERIC-FIELD-2.
Figure 9.4 The INSPECT statement 01 RECORD-IN. 05 SOC-SEC-NUM PIC 9(9). . . .01 PRINT-LINE. 05 SOC-SEC-NUM-OUT PIC 999B99B9999. . . . PROCEDURE DIVISION. . . . MOVE SOC-SEC-NUM TO SOC-NUM-OUT. INSPECT SOC-SEC-NUM-OUT REPLACING ALL ‘ ‘ BY ‘-’. Initial: SOC-SEC-NUM 123 45 6789 Result: SOC-SEC-NUM-OUT 123-45-6789
Before Execution LAST-NAME S M I T H MIDDLE-INITIAL H FIRST-NAME J O H N ENTIRE-NAME Figure 9.5 The STRING Statement 05 NAME-IN-PIECES. 10 LAST-NAME PIC X(16). 10 FIRST-NAME PIC X(10). 10 MIDDLE-INITIAL PIC X. 05 ENTIRE-NAME PIC X(29). (a) Working-Storage Holding Areas
(1) ENTIRE-NAME J O H N (2) ENTIRE-NAME J O H N (b) (3) ENTIRE-NAME J O H N (b) H (4) ENTIRE-NAME J O H N (b) H (b) (5) ENTIRE-NAME J O H N (b) H (b) S M I T H Figure 9.5 The STRING Statement (cont.) MOVE SPACES TO ENTIRE-NAME.STRING FIRST-NAME DELIMITED BY SPACE ‘ ‘ DELIMITED BY SIZE MIDDLE-INITIAL DELIMITED BY SPACE ‘ ‘ DELIMITED BY SIZE LAST-NAME DELIMITED BY SPACE INTO ENTIRE-NAME (b) STRING Statement Execution Steps (c) Sequence of transfer
Before Execution LAST-NAME MIDDLE-INITIAL FIRST-NAME ENTIRE-NAME J O H N (b) H (b) S M I T H Figure 9.6 The UNSTRING Statement 05 NAME-IN-PIECES. 10 LAST-NAME PIC X(16). 10 FIRST-NAME PIC X(10). 10 MIDDLE-INITIAL PIC X. 05 ENTIRE-NAME PIC X(31). (a) Working-Storage Holding Areas MOVE SPACES TO NAME-IN-PIECES.UNSTRING ENTIRE-NAME DELIMITED BY ‘ ‘ INTO FIRST-NAME MIDDLE-INITIAL LAST-NAME. (b) UNSTRING Statement
J O H N S M I T H (1) ENTIRE-NAME J O H N (b) H (b) S M I T H Figure 9.6 The UNSTRING Statement (1) ENTIRE-NAME J O H N (b) H (b) S M I T H H FIRST-NAME MIDDLE-INITIAL LAST-NAME (c) Execution of UNSTRING
Figure 9.7 Reference Modification 01 INCOMING-RECORD. . . . 05 TELEPHONE-NUMBER PIC X(10). . . . 01 EDITED-PHONE-NUMBER. 05 FILLER PIC X VALUE ‘(‘. 05 AREA-CODE PIC X(3). 05 FILLER PIC X VALUE ‘)’. 05 EXCHANGE PIC X(3). 05 FILLER PIC X VALUE ‘-’. 05 DIGITS PIC X(4). . . . MOVE TELEPHONE-NUMBER (1:3) TO AREA-CODE. MOVE TELEPHONE-NUMBER (4:3) TO EXCHANGE. MOVE TELEPHONE-NUMBER (7:4) TO DIGITS.