Structured COBOL Programming 10 th edition John Wiley
Structured COBOL Programming 10 th edition John Wiley & Sons, Inc. Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout Power. Point Presentation Winifred J. Rex Bowling Green State University 1
Chapter 7 Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions 7 -2
Chapter Objectives To familiarize you with • Ways in which arithmetic may be performed in COBOL • Formats and options available with arithmetic verbs 3
Chapter Contents • • Basic Arithmetic Verbs Options Available with Arithmetic Verbs COMPUTE Statement Signed Numbers in Arithmetic Operations • Intrinsic Functions 4
Basic Arithmetic Verbs • ADD, SUBTRACT, MULTIPLY, DIVIDE • All require fields operated on to – Have numeric PICTURE clauses – Contain numeric data when statements executed 5
ADD … TO Statement Format 1 ADD identifier-1 literal-1 … TO identifier-2. . . • identifier-1 or literal-1 added to identifier -2 • Result stored in identifier-2 • What does the syntax tell you? 6
ADD … TO Examples Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD Statement Add X To Y Add X, 9 To Y Add X, 6, Y To Z Result Y=8 Y = 17 Z = 21 7
ADD … TO Statement • Identifiers preceding TO are unchanged • Value of identifier after TO – Used in ADD operation – Original value replaced with ADD result 8
ADD … GIVING Statement Format 2 ADD identifier-1 literal-1 … GIVING identifier-2. . . • Identifiers and literals preceding GIVING added together • Result stored in identifier-2 • Identifier-1 and literal-1 are unchanged! 9
ADD … GIVING Examples Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD Statement Result Add X, Y Giving Z Z=8 Add X, 10 Giving Y Y = 15 Add X, 4, Y Giving Z Z = 12 X, Y, 10, etc. are all unchanged! 10
ADD … GIVING Statement • Identifiers preceding GIVING are unchanged • Value of identifier after GIVING – Not part of ADD operation – Original value replaced with ADD result – May be report-item with edit symbols 11
ADD … GIVING Statement • TO may be included before last identifier or literal preceding GIVING • For example: Add X, 4 To Y Giving Z – Adds values of X, 4 and Y together – Stores result in Z – X, 4, and Y are UNCHANGED! 12
ADD Statement • Comma followed by one space may be used to separate operands • Result of ADD always placed in field(s) after TO or GIVING • Result field must be data-name, not a literal 13
Producing More Than One Sum • Several ADD operations can be done in single statement • Assume X, Y and Z are numeric fields X = 5, Y = 3 and Z = 7 ADD Statement Result Add X To Y, Z Y = 8, Z = 12 Value of X is added to Y and Z Add X, 6 Giving Y, Z Y = 11, Z = 11 Result of the addition of X and 6 are MOVED to Y and Z. Former values of Y and Z do NOT participate in this operation. 14
ADD … TO vs ADD … GIVING • Use ADD … TO when original contents of result operand – Need to be included in operation – But are not needed after operation – All operands must be numeric. • Use ADD … GIVING when – Original contents of all operands except result field are to be retained – Operands following the GIVI NG need NOT be numeric and will frequently be reportitem fields. 15
SUBTRACT Statement Format 1 SUBTRACT identifier-1 literal-1 … FROM identifier-2. . . • identifier-1 or literal-1 subtracted from identifier-2 • Result stored in identifier-2 16
SUBTRACT Examples Assume A, B and C are numeric fields A = 6, B = 2 and C = 18 SUBTRACT Statement Subtract A From C Subtract B, 5 From C Subtract B From A, C Result C = 12 C = 11 A = 4, C = 16 17
SUBTRACT Statement Rules • All identifiers and literals must be numeric • Data-name, not a literal, must follow FROM • All fields, literals preceding FROM added together • Sum subtracted from field after FROM • Result stored in field after FROM 18
SUBTRACT … GIVING Statement Format 2 SUBTRACT identifier-1 literal-1 … FROM identifier-2 literal-2 GIVING identifier-3. . . • identifier-1 or literal-1 subtracted from identifier-2 or literal-2 • Result stored in identifier-3 • Only Operand changed is identifier-3! 19
SUBTRACT … GIVING Examples Assume A, B and C are numeric fields A = 6, B = 2 and C = 18 SUBTRACT Statement Subtract B From A Giving C Subtract A From 15 Giving C Subtract A, 4 From C Giving B Result C=4 C=9 B=8 20
SUBTRACT … GIVING Rules • All identifiers, literals before FROM must be numeric • GIVING must be followed by data-name – May be numeric or report-item – remember, GIVING really is a Move results here • All fields, literals preceding FROM added together • Sum subtracted from field after FROM – BUT ‘from field’ is NOT changed!! • Result stored in field after GIVING 21
MULTIPLY Statement Format 1 MULTIPLY identifier-1 literal-1 BY identifier-2. . . • identifier-1 or literal-1 multiplied by identifier-2 • Result stored in identifier-2 22
MULTIPLY Examples Assume Q, R and S are numeric fields Q = 4, R = 7 and S = 5 MULTIPLY Statement Multiply Q By R Multiply 10 By S Multiply 2 By R, S Result R = 28 S = 50 R = 14, S = 10 23
MULTIPLY … GIVING Statement Format 2 MULTIPLY identifier-1 literal-1 BY identifier-2 literal-2 GIVING identifier-3. . . • identifier-1 or literal-1 multiplied by identifier-2 or literal-2 • Result stored in identifier-3 • Must be careful here!!! (more ahead!) 24
MULTIPLY … GIVING Examples Assume Q, R and S are numeric fields Q = 4, R = 7 and S = 5 MULTIPLY Statement Multiply Q By R Giving S Multiply Q By 3 Giving S Multiply 6 By Q Giving R, S Result S = 28 S = 12 R = 24 S = 24 25
MULTIPLY Statement • Only two operands can be multiplied • To obtain product of 3 operands requires two instructions To find Price x Qty x Discount Multiply Price By Qty Giving WS-Amt Multiply Discount By WS-Amt 26
DIVIDE Statement Format 1 DIVIDE identifier-1 literal-1 INTO identifier-2. . . • identifier-1 or literal-1 divided into identifier-2 • Result stored in identifier-2 27
DIVIDE Examples Assume X, Y and Z are numeric fields X = 2, Y = 12 and Z = 8 DIVIDE Statement Divide X Into Y Divide 3 Into Y Divide 2 Into Y, Z Result Y=6 Y=4 Y = 6, Z = 4 28
DIVIDE … GIVING Statement Format 2 DIVIDE identifier-1 literal-1 INTO BY identifier-2 literal-2 GIVING identifier-3. . . • identifier-1 or literal-1 divided into or by identifier-2 or literal-2 • Result stored in identifier-3 29
DIVIDE … GIVING Examples Assume X, Y and Z are numeric fields X = 2, Y = 12 and Z = 8 DIVIDE Statement Divide X Into Y Giving Z Divide Y By X Giving Z Divide 16 By Z Giving X, Y Result Z=6 X=2 Y=2 Note: only receiving fields are changed!! 30
REMAINDER Clause • Optional clause with DIVIDE used to store remainder of division • Assume Q and R have PICTUREs of 99 Divide 70 By 15 Giving Q Remainder R • Stores quotient 4 in Q and integer remainder 10 in R 31
ROUNDED Option • Arithmetic result truncated if room to store all decimal positions not available – Result of 3. 89 stored in field with PIC 9 V 9 as 3^8 • Include ROUNDED to round result to PICTURE specification – Result of 3. 89 stored as 3. 9 if ROUNDED option used 32
ROUNDED Option Examples 01 Amt 1 01 Amt 2 Pic 9 V 99 Pic 999. Value 2. 25. Arithmetic statement Multiply. 3 By Amt 1 Rounded Divide 150 By 9 Giving Amt 2 Rounded Result. 675 Value Stored Amt 1 = 0^67 Amt 1 = 0^68 16. 66… Amt 2 = 017 33
Overflow or Size Error • Occurs when result value too large to be stored in result field • Result of this ADD statement is 1, 075 Add 350 To 725 Giving Num • If Num has PICTURE of 999, only 3 digits can be stored • High-order digits truncated so 075 stored in Num 34
Checking for Overflow • Any arithmetic statement may include one or both size error clauses • ON SIZE ERROR statement(s) – Specifies one or more statements to be executed if overflow (size error) occurs • NOT ON SIZE ERROR statement(s) – Specifies one or more statements to be executed if overflow (size error) does not occur 35
SIZE ERROR Clause Example Add X To Y Giving Z On Size Error Display ' Result too large' Not On Size Error Perform Calc-Para End-Add • If sum of X and Y too large to store in Z, Display statement executed • If Z large enough for result, Calc-Para is performed 36
SIZE ERROR Clauses • When using one or both clauses, use scope terminator to end arithmetic operation – END-ADD, END-SUBTRACT – END-MULTIPLY, END-DIVIDE • If you do not? Multiply A by B on size error … … … end-multiply 37
Size of Receiving Fields • Ensure receiving field has PICTURE large enough to store result • Addition - define resultant field one position larger than largest field added • Subtraction - define resultant field as large as number being subtracted from – Assumes positive numbers – Assumes smaller subtracted from larger number 38
Size of Receiving Fields • Multiplication - define resultant field equal to sum of lengths of operands begin multiplied • Division - define resultant field equal to sum of number of digits in divisor and dividend • Go through examples!!! 39
COMPUTE Statement • General arithmetic statement using symbols in place of arithmetic verbs Symbol Verb + ADD SUBTRACT * MULTIPLY / DIVIDE ** exponentiation 40
COMPUTE Statement – simple format Format COMPUTE identifier-1 … = arithmetic-exp-1 literal-1 identifier-2 • Identifier to left of equal sign set to value of arithmetic-expression, literal or identifier on right of equal sign 41
COMPUTE Examples Assume X, Y and Z are numeric fields X = 9, Y = 4 and Z = 12 COMPUTE Statement Result Compute Z = X * Y Z = 36 Compute X = Z - Y + 2 X = 10 Compute X = Y X=4 Compute Z = Y ** 2 Z = 16 Resultant field need NOT be numeric!!!!! May use both: Computer X, X-Out = … 42
Order of Evaluation • Arithmetic expression may include any combination of symbols +, -, *, / or ** • Order of operations 1. 2. 3. 4. ** * or / + or () all exponentiation performed first in order or appearance left to right override rules 1 -3, all operations in ( ) performed first Know these rules; Practice examples!!! 43
COMPUTE Examples Assume X, Y and Z are numeric fields X = 6, Y = 18 and Z = 5 What are the order of operations? ? ? COMPUTE Statement Compute Z = Y / X + 3 Compute Z = Y / (X + 3) Compute Y = Z + X * 10 Compute Y = Z * X / 10 Result Z=6 Z=2 Y = 65 Y=3 44
COMPUTE Statement • COMPUTE can include same optional clauses used with other arithmetic verbs • ROUNDED follows result field (identifier preceding equal sign) • If ON SIZE ERROR or NOT ON SIZE ERROR clauses used, include scope terminator END-COMPUTE • When you add ON Size Error or Not On Size Error, you are making an imperative statement a ‘conditional statement’ and thus the scope terminator needs to show the scope of the condition. 45
Signed Numbers • Use S in PIC clause of result field if – Numbers used in calculation may be negative – Calculation may produce negative results • PIC clause without S assumed to be unsigned – If negative result stored in unsigned field, sign not retained 46
Intrinsic Functions • Built-in procedures to perform particular task like – Find square root of number – Convert letters to uppercase – Get current date • Approved as extensions to COBOL standard in 1989 • Now included in many compilers 47
Intrinsic Functions Example Find square root of X and place result in Y Compute Y = Function Sqrt(X) • Value of X passed to function called Sqrt • Code in function finds square root of X • Result returned by Sqrt assigned to Y • Used to have to say (…) ** (. 5) 48
Intrinsic Functions Example Convert More-Data to uppercase Move Function Upper-Case(More-Data) To Up-More-Data • If More-Data = "Yes", function Upper. Case returns value "YES" • Value "YES" moved to Up-More-Data 49
Intrinsic Functions – format!! Format FUNCTION function-name (argument) • Argument - input to function – May be numeric or alpha-numeric depending on function – Functions may have 0, 1 or more arguments. 50
Intrinsic Functions • Output of function - result returned after function performs its task • Function returning alphanumeric result used in statements using alphanumeric data-items (like Upper. Case) • Function returning numeric result can be used only in arithmetic expressions like SQRT( ) 51
COBOL 2002+ Changes • Spaces around arithmetic operators will no longer be required • COMPUTE statement will yield same results on all compilers – Will make precision or number of decimal places in each intermediate calculation fixed 52
Chapter Summary • ADD, SUBTRACT, MULTIPLY, and DIVIDE verbs – format without GIVING • Receiving field is part of arithmetic • May not be report-item – with GIVING format • Receiving field is not part of arithmetic • May be report-item 53
Chapter Summary • COMPUTE used for any combination of arithmetic operations • Order of evaluation of operators 1. 2. 3. 4. ** * or / in sequence left to right + or - in sequence left to right ( ) override normal hierarchy rules 54
Chapter Summary • ROUNDED can follow receiving field in any arithmetic verb • ON SIZE ERROR, NOT ON SIZE ERROR – Can be used with any arithmetic verb – Include scope terminator (e. g. , END-ADD) 55
Chapter Summary • Intrinsic functions added as COBOL extensions in 1989 – Calendar – Numerical analysis – Statistical – Trigonometric – Financial – Character and String 56
Copyright © 2003 John Wiley & Sons, Inc. All rights reserved. Reproduction or translation of this work beyond that permitted in Section 117 of the 1976 United States Copyright Act without the express written permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages, caused by the use of these programs or from the use of the information contained herein. 57
- Slides: 57