1 / 65

CHAPTER 7 COMPUTING IN COBOL: THE ARITHMETIC VERBS AND INTRINSIC FUNCTIONS

CHAPTER 7 COMPUTING IN COBOL: THE ARITHMETIC VERBS AND INTRINSIC FUNCTIONS. THE BASIC ARITHMETIC VERBS. All the basic arithmetic operations of ADD, SUBTRACT, MULTIPLY , and DIVIDE require that the fields operated on (operands): Have numeric PIC clauses

altessa
Download Presentation

CHAPTER 7 COMPUTING IN COBOL: THE ARITHMETIC VERBS AND INTRINSIC FUNCTIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 7COMPUTING IN COBOL: THE ARITHMETIC VERBS AND INTRINSIC FUNCTIONS Structured COBOL Programming, Stern & Stern, 9th Edition

  2. THE BASIC ARITHMETIC VERBS • All the basic arithmetic operations of ADD, SUBTRACT, MULTIPLY, and DIVIDE require that the fields operated on (operands): • Have numeric PIC clauses • Actually have numeric data when the program is executed. • All literals are numeric Structured COBOL Programming, Stern & Stern, 9th Edition

  3. THE BASIC ARITHMETIC VERBS The Resultant Field • The result of an basic arithmetic operation is always placed in the last field mentioned. • The only field that is altered as a result of the an operation is this last field, • This is the field directly following the word TO, BY, INTO, FROM when using Format 1 or GIVING when using Format 2. Structured COBOL Programming, Stern & Stern, 9th Edition

  4. THE BASIC ARITHMETIC VERBS The Resultant Field - In all cases, the resultant field must be an identifier or data-name, not a literal. - The statement • ADD HOURS-WORKED TO 40 • SUBTRACT AGE FROM 40 • are incorrect. • When using Format 1 in an arithmetic statement, all the data-names and literals are computational combined and the result is placed in the last field. Structured COBOL Programming, Stern & Stern, 9th Edition

  5. DEBUGGING TIP • A comma can be used anywhere in an instruction, as long as at least one space follows it. • However, this not really recommended since this adds characters that can cause errors. • It is preferred that you should separate entries instead by placing them on individual lines. Structured COBOL Programming, Stern & Stern, 9th Edition

  6. ADD STATEMENT An example of a proper ADD is: ADD HOURS-WORKED TO WEEKLY-HOURS • The fields HOURS-WORKED and WEEKLY-HOURS are added together, and the result is placed in the last field specified--WEEKLY-HOURS. Structured COBOL Programming, Stern & Stern, 9th Edition

  7. SUBTRACT STATEMENT Examples: 1. SUBTRACT 15.40 TAX TOTAL FROM AMT 2. SUBTRACT 15.40 TAX TOTAL FROM AMT GIVING NET • Examples 1 and 2 produce the same result but in different storage areas. • In Example 2, the original contents of NET are replaced with the result and do not affect the calculation. Structured COBOL Programming, Stern & Stern, 9th Edition

  8. MULTIPLY and DIVIDE Statements Multiply Examples: 1. MULTIPLY HOURS-WORKED BY HOURLY-RATE 2. MULTIPLY QTY BY PRICE 3. MULTIPLY 2000 BY NO-OF-EXEMPTIONS GIVING EXEMPTION-AMT 4. MULTIPLY 60 BY HOURS GIVING MINUTES Structured COBOL Programming, Stern & Stern, 9th Edition

  9. MULTIPLY and DIVIDE Statements DIVIDE EXAMPLES: 1. DIVIDE MINUTES INTO HOURS 2. DIVIDE 60 INTO MINUTES GIVING HOURS 3. DIVIDE ANN-SAL-IN BY 12 GIVING MONTHLY-SAL-OUT Structured COBOL Programming, Stern & Stern, 9th Edition

  10. EXAMPLES OF ARITHMETIC OPERATIONS Example 2: Compute the average of three fields, EXAM1, EXAM2, EXAM3. Place the answer in AVERAGE without altering the contents of the three fields. One possible solution is: ADD EXAM 1 EXAM 2 EXAM 3 GIVING AVERAGE DIVIDE 3 INTO AVERAGE Structured COBOL Programming, Stern & Stern, 9th Edition

  11. MULTIPLY and DIVIDE Statements DIVIDE EXAMPLES: • DIVIDE ANN-SAL-IN BY 12 GIVING MONTHLY-SAL-OUT • BY requires GIVING • All arithmetic operations can have more than one resultant field. • Although ADD and SUBTRACT instructions can operate on numerous fields • MULTIPLY and DIVIDE instructions are limited in the number of operations performed. Structured COBOL Programming, Stern & Stern, 9th Edition

  12. ADD - Format 1 • ADD {identifier-1} T0 {identifier-2} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-ADD] • Contents of identifier-2 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  13. ADD - Format 2 • ADD {identifier-1} T0 {identifier-2} GIVING {identifier-3} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-ADD] • Contents of identifier-3 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  14. 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 Structured COBOL Programming, Stern & Stern, 9th Edition

  15. 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 1.23 4.56 5.7 ADD A B GIVING C ROUNDED 123 456 58 1.23 4.56 5.8 ROUNDED always follows the result field Structured COBOL Programming, Stern & Stern, 9th Edition

  16. ON SIZE ERROR • Size errors occur when result field is not larger enough to receive calculated value • Also called overflow • Example:05 Pay pic 9(3).05 Rate pic 99 value 50.Multiply 45 by Rate Giving PayPay will equal 250 not 2250 Structured COBOL Programming, Stern & Stern, 9th Edition

  17. ON SIZE ERROR • Will trap size error problems and execute the imperative statements that follow it • If a size error occurs result field will be set to zero • Protects against divide by zero errors Structured COBOL Programming, Stern & Stern, 9th Edition

  18. Determining Result Field Size • Addition - result should be one larger that the number of digits in the largest field being added • Subtraction - result should be as large as the minuend (the number being subtracted from) Structured COBOL Programming, Stern & Stern, 9th Edition

  19. Determining Result Field Size • Multiplication - result should equal the sum of the lengths of the fields being multiplied • Division - result should equal the sum of the number of digits in the dividend and the divisor Structured COBOL Programming, Stern & Stern, 9th Edition

  20. ADD STATEMENT Adding More Than Two Fields • You are not restricted to two operands when using an ADD operation: ADDAMT1 AMT2 AMT3 GIVING AMT4 • The original contents of AMT4, the resultant field, are destroyed and have no effect on the ADD operation. • The three operands AMT1, AMT2, and AMT3 are unchanged. Structured COBOL Programming, Stern & Stern, 9th Edition

  21. ADD STATEMENT Example 8: ADD AMT1 AMT2 AMT3 TO AMT4 • AMT1, AMT2, and AMT3 are added to the original contents of AMT4. The result is placed in AMT4. • AMT1, AMT2, AMT3 are unchanged just as in the GIVING option. Structured COBOL Programming, Stern & Stern, 9th Edition

  22. ADD STATEMENT • Producing More Than One Sum • It is also possible to perform several ADD operations with a single statement, using the TO format. • The following is a valid statement: ADD AMT1 AMT2 TO TOTAL1 TOTAL2 • This results are the same as: ADDAMT1 AMT2TOTOTAL1 ADDAMT1 AMT2TOTOTAL2 Structured COBOL Programming, Stern & Stern, 9th Edition

  23. SUBTRACT STATEMENT • It is possible to perform several SUBTRACT operations with a single statement using Format 1: The following is a valid statement: SUBTRACT AMT1 AMT2 AMT3 FROM TOTAL1 TOTAL2 TOTAL3 Same as SUBTRACT AMT1 AMT2 AMT3 FROM TOTAL1 SUBTRACT AMT1 AMT2 AMT3 FROM TOTAL2 SUBTRACT AMT1 AMT2 AMT3 FROM TOTAL3 Structured COBOL Programming, Stern & Stern, 9th Edition

  24. SUBTRACT - Format 1 • SUBTRACT {identifier-1} FROM {identifier-2} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-SUBTRACT] • Contents of identifier-2 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  25. SUBTRACT - Format 2 • SUBTRACT {identifier-1} FROM {identifier-2} GIVING {identifier-3} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-SUBTRACT] • Contents of identifier-3 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  26. 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 Structured COBOL Programming, Stern & Stern, 9th Edition

  27. MULTIPLY - Format 1 • MULTIPLY {identifier-1} BY {identifier-2} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-MULTIPY] • Contents of identifier-2 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  28. MULTIPLY - Format 2 • MULTIPLY {identifier-1} BY {identifier-2} GIVING {identifier-3} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-MULTIPLY] • Contents of identifier-3 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  29. Table 5.9 The MULTIPLY Instruction DATA NAME A B C Value before execution 5 10 30 Value after execution of: MULTIPLY A BY B 5 50 15 MULTIPLY B BY A 50 10 30 MULTIPLY B BY A GIVING C 5 10 50 MULTIPLY A BY B GIVING C 5 10 50 MULTIPLY A BY 3 GIVING B C 5 15 15 Structured COBOL Programming, Stern & Stern, 9th Edition

  30. DIVIDE - Format 1 • DIVIDE {identifier-1} INTO {identifier-2} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-DIVIDE] • Contents of identifier-2 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  31. DIVIDE - Format 2 • DIVIDE {identifier-1} INTO {identifier-2} GIVING {identifier-3} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-DIVIDE] • Contents of identifier-3 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  32. DIVIDE - Format 3 • DIVIDE {identifier-1} BY {identifier-2} GIVING {identifier-3} [ROUNDED] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-DIVIDE] • Contents of identifier-3 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  33. DIVIDE - Format 4 • DIVIDE {identifier-1} INTO {identifier-2} GIVING {identifier-3} [ROUNDED][REMAINDER identifier-4] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-DIVIDE] • Contents of identifier-3 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  34. DIVIDE - Format 5 • DIVIDE {identifier-1} BY {identifier-2} GIVING {identifier-3} [ROUNDED][REMAINDER identifier-4] [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-DIVIDE] • Contents of identifier-3 are changed Structured COBOL Programming, Stern & Stern, 9th Edition

  35. 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 Structured COBOL Programming, Stern & Stern, 9th Edition

  36. DIVIDE Statements Use of the REMAINDER Clause in the DIVIDE Operation Example 1: DIVIDE 130 BY 40 GIVING WS-TOTAL REMAINDER WS-REM • WS-TOTAL has a PICTURE of 99. After the operation is performed, 03 is placed in WS-TOTAL. • The remainder of the division can be saved by using the REMAINDER clause 10 is place in WS-REM Structured COBOL Programming, Stern & Stern, 9th Edition

  37. EXAMPLES OF ARITHMETIC OPERATIONS Example 1: Celsius temperatures are to be converted to Fahrenheit temperatures according to the following formula: FAHRENHEIT= (9/5) CELSIUS +32 One solution is as follows: MULTIPLY 9 BY CELSIUS DIVIDE 5 INTO CELSIUS ADD 32 CELSIUS GIVING FAHRENHEIT Structured COBOL Programming, Stern & Stern, 9th Edition

  38. DEBUGGING WITH COBOL 85 • When using a separate clause such as ON SIZE ERROR, use a scope terminator with COBOL 85 to delimit or end the arithmetic operation. • END-ADD, END-SUBTRACT, END-MULTIPLY, and END-DIVIDE are all permissible scope terminators. Structured COBOL Programming, Stern & Stern, 9th Edition

  39. GIVING • When using the GIVING format, all fields and literals preceding the word GIVING are added together and the sum is placed in the field following the word GIVING. • Thus, when using the GIVING format, the last data field is not part of the operation. • Because it is not part of the arithmetic operation, it can be a report-item with edit symbols, as seen below: ADD HOURS-WORKED WEEKLY-HOURS GIVING TOTAL-HOURS. Structured COBOL Programming, Stern & Stern, 9th Edition

  40. GIVING Deciding Whether to Use the GIVING Format • Use the GIVING format 2 when the contents of operands are to be retained. • When you will no longer need the original contents of an operand format 1 may be used. Structured COBOL Programming, Stern & Stern, 9th Edition

  41. Simple Arithmetic Review

  42. Arithmetic Verb Template • Most COBOL arithmetic verbs conform to the template above. For example; ADD Takings TO CashTotal. ADD Males TO Females GIVING TotalStudents. SUBTRACT Tax FROM GrossPay. SUBTRACT Tax FROM GrossPay GIVING NetPay. DIVIDE Total BY Members GIVING MemberAverage. DIVIDE Members INTO Total GIVING MemberAverage. MULTIPLY 10 BY Magnitude. MULTIPLY Members BY Subs GIVING TotalSubs. • The exceptions are the COMPUTEand the DIVIDE with REMAINDER. Structured COBOL Programming, Stern & Stern, 9th Edition

  43. The Divide Exception 020 001 • DIVIDE 201 BY 10 GIVING Quotient REMAINDER Remain. • Before 209 424 • After

  44. The ON SIZE ERROR option Receiving Field Actual Result SIZE ERROR PIC 9(3)V9. 245.96 PIC 9(3)V9. 1245.9 PIC 9(3). 124 PIC 9(3). 1246 PIC 9(3)V9 Not Rounded 124.45 PIC 9(3)V9 Rounded 124.45 PIC 9(3)V9 Rounded 3124.45 Yes Yes No Yes Yes No Yes • A size error condition exists when, after decimal point alignment, the result is truncated on either the left or the righthand side. • If an arithmetic statement has a rounded phrase then a size error only occurs if there is truncation on the left hand side (most significant digits).

  45. THE COMPUTE STATEMENT Structured COBOL Programming, Stern & Stern, 9th Edition

  46. BASIC FORMAT • If complex or extensive arithmetic operations are required in a program the use of the four arithmetic verbs may prove cumbersome. • The COMPUTEverb provides another method of performing arithmetic. The COMPUTE statement uses arithmetic symbols rather than arithmetic verbs. Structured COBOL Programming, Stern & Stern, 9th Edition

  47. Mathematical Operators • ** Exponentiation X3 = X ** 3 • * Multiplication X * Y • / Division X / Y • + Addition X + Y • - Subtraction X - Y • Order of Operations - PEMDAS • () parenthesis change the order of operations Structured COBOL Programming, Stern & Stern, 9th Edition

  48. SYMBOLS USED IN A COMPUTE The following examples illustrate the use of the COMPUTE verb: 1. COMPUTE TAX = .05 * AMT 2. COMPUTE DAILY-SALES = QTY * UNIT-PRICE / 5 3. COMPUTE NET = AMT - .05 * AMT Structured COBOL Programming, Stern & Stern, 9th Edition

  49. COMPUTE • COMPUTE {identifier-1} [ROUNDED] = expression-1 [ON SIZE ERROR] imperative statements [NOT ON SIZE ERROR] imperative statements[END-COMPUTE] Structured COBOL Programming, Stern & Stern, 9th Edition

  50. COMPUTE BASIC FORMAT • The COMPUTE statement has a data-name or identifier to the left of the equal sign. • The value computed in the arithmetic expression to the right of the equal sign is placed in the field preceding the equal sign. • The fields specified after the equal sign in a COMPUTE statement may be numeric literals or data-names with numeric PIC clauses. • The COMPUTE statement may include more than one operation. Structured COBOL Programming, Stern & Stern, 9th Edition

More Related