7-1 cobol for the 21 st century nancy stern hofstra university robert a. stern nassau community...

Post on 14-Dec-2015

233 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

7-1

COBOL for the 21st Century

Nancy Stern Hofstra University

Robert A. Stern Nassau Community College

James P. Ley University of Wisconsin-Stout (Emeritus)

John Wiley & Sons, Inc.11th edition

7-2

Chapter Contents

• Basic Arithmetic Verbs

• Options Available with Arithmetic Verbs

• COMPUTE Statement

• Signed Numbers in Arithmetic Operations

• Intrinsic Functions

7-3

Basic Arithmetic Verbs

• ADD, SUBTRACT, MULTIPLY, DIVIDE

• All require fields operated on to– Have numeric PICTURE clauses– Contain numeric data when statements

executed

7-4

ADD … TO Statement

identifier-1ADD … TO identifier-2 ...

literal-1

• identifier-1 or literal-1 added to identifier-2

• Result stored in identifier-2

Format 1

7-5

ADD … TO Examples

Assume X, Y and Z are numeric fieldsX = 5, Y = 3 and Z = 7

ADD Statement ResultAdd X To Y Y = 8Add X, 9 To Y Y = 17Add X, 6, Y To Z Z = 21

7-6

ADD … TO Statement

• Identifiers preceding TO are unchanged• Value of identifier after TO

– Used in ADD operation– Original value replaced with ADD result

7-7

ADD … GIVING Statement

identifier-1ADD … GIVING identifier-

2 ... literal-1

• Identifiers and literals preceding GIVING added together

• Result stored in identifier-2

Format 2

7-8

ADD … GIVING Examples

Assume X, Y and Z are numeric fieldsX = 5, Y = 3 and Z = 7

ADD Statement ResultAdd X, Y Giving Z Z = 8Add X, 10 Giving Y Y = 15Add X, 4, Y Giving Z Z = 12

7-9

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

7-10

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

7-11

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

7-12

Producing More Than One Sum

• Several ADD operations can be done in single statement

• Assume X, Y and Z are numeric fieldsX = 5, Y = 3 and Z = 7

ADD Statement ResultAdd X To Y, Z Y = 8, Z = 12Add X, 6 Giving Y, Z Y = 11, Z = 11

7-13

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

• Use ADD … GIVING when– Original contents of all operands except

result field are to be retained

7-14

SUBTRACT Statement

identifier-1SUBTRACT … FROM identifier-2 ...

literal-1

• identifier-1 or literal-1 subtracted from identifier-2

• Result stored in identifier-2

Format 1

7-15

SUBTRACT Examples

Assume A, B and C are numeric fields A = 6, B = 2 and C = 18

SUBTRACT Statement ResultSubtract A From C C = 12Subtract B, 5 From C C = 11Subtract B From A, C A = 4, C = 16

7-16

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

7-17

SUBTRACT … GIVING Statement

identifier-1 identifier-2

SUBTRACT … FROM literal-1 literal-2

GIVING identifier-3 ...

• identifier-1 or literal-1 subtracted from identifier-2 or literal-2

• Result stored in identifier-3

Format 2

7-18

SUBTRACT … GIVING Examples

Assume A, B and C are numeric fields A = 6, B = 2 and C = 18

SUBTRACT Statement ResultSubtract B From A Giving C C = 4Subtract A From 15 Giving C C = 9Subtract A, 4 From C Giving B B = 8

7-19

SUBTRACT … GIVING Rules

• All identifiers, literals before FROM must be numeric

• GIVING must be followed by data-name– May be numeric or report-item

• All fields, literals preceding FROM added together

• Sum subtracted from field after FROM

• Result stored in field after GIVING

7-20

MULTIPLY Statement

identifier-1MULTIPLY BY identifier-2 ...

literal-1

• identifier-1 or literal-1 multiplied by identifier-2

• Result stored in identifier-2

Format 1

7-21

MULTIPLY Examples

Assume Q, R and S are numeric fields Q = 4, R = 7 and S = 5

MULTIPLY Statement ResultMultiply Q By R R = 28Multiply 10 By S S = 50 Multiply 2 By R, S R = 14, S = 10

7-22

MULTIPLY … GIVING Statement

identifier-1 identifier-2

MULTIPLY BY literal-1 literal-2

GIVING identifier-3 ...

• identifier-1 or literal-1 multiplied by identifier-2 or literal-2

• Result stored in identifier-3

Format 2

7-23

MULTIPLY … GIVING Examples

Assume Q, R and S are numeric fields Q = 4, R = 7 and S = 5

MULTIPLY Statement ResultMultiply Q By R Giving S S = 28Multiply Q By 3 Giving S S = 12Multiply 6 By Q Giving R, S R = 24

S = 24

7-24

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-AmtMultiply Discount By WS-Amt

7-25

DIVIDE Statement

identifier-1DIVIDE INTO identifier-2 ...

literal-1

• identifier-1 or literal-1 divided into identifier-2

• Result stored in identifier-2

Format 1

7-26

DIVIDE Examples

Assume X, Y and Z are numeric fieldsX = 2, Y = 12 and Z = 8

DIVIDE Statement ResultDivide X Into Y Y = 6Divide 3 Into Y Y = 4 Divide 2 Into Y, Z Y = 6, Z = 4

7-27

DIVIDE … GIVING Statement

identifier-1 INTO identifier-2

DIVIDE literal-1 BY literal-2

GIVING identifier-3 ...

• identifier-1 or literal-1 divided into or by identifier-2 or literal-2

• Result stored in identifier-3

Format 2

7-28

DIVIDE … GIVING Examples

Assume X, Y and Z are numeric fieldsX = 2, Y = 12 and Z = 8

DIVIDE Statement ResultDivide X Into Y Giving Z Z = 6Divide Y By X Giving Z Z = 6Divide 16 By Z Giving X, Y X = 2

Y = 2

7-29

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

7-30

ROUNDED Option

• Arithmetic result truncated if room to store all decimal positions not available– Result of 3.89 stored in field with PIC 9V9

as 3^8

• Include ROUNDED to round result to PICTURE specification– Result of 3.89 stored as 3.9 if ROUNDED

option used

7-31

ROUNDED Option

01 Amt1 Pic 9V99 Value 2.25.

01 Amt2 Pic 999.

Arithmetic statement Result Value Stored

Multiply .3 By Amt1 .675 Amt1 = 0^67

Multiply .3 By Amt1 Rounded .675 Amt1 = 0^68

Divide 150 By 9Giving Amt2 Rounded 16.66… Amt2 = 017

Examples

7-32

Overflow or Size Error

• Occurs when result value too large to be stored in result field

• Result of this ADD statement is 1,075Add 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

7-33

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

7-34

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

7-35

SIZE ERROR Clauses

• When using one or both clauses, use scope terminator to end arithmetic operation– END-ADD, END-SUBTRACT – END-MULTIPLY, END-DIVIDE

7-36

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

7-37

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

7-38

COMPUTE Statement

• General arithmetic statement using symbols in place of arithmetic verbs

Symbol Verb+ ADD- SUBTRACT* MULTIPLY/ DIVIDE** exponentiation

7-39

COMPUTE Statement

arithmetic-exp-1COMPUTE identifier-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

Format

7-40

COMPUTE Examples

Assume X, Y and Z are numeric fieldsX = 9, Y = 4 and Z = 12

COMPUTE Statement ResultCompute Z = X * Y Z = 36Compute X = Z - Y + 2 X = 10Compute X = Y X = 4Compute Z = Y ** 2 Z = 16

7-41

Order of Evaluation• Arithmetic expression may include any

combination of symbols +, -, *, / or ** • Order of operations

1. ** all exponentiation performed first2. * or / in order or appearance left to right3. + or - in order or appearance left to right4. ( ) override rules 1-3, all operations

in ( ) performed first

7-42

COMPUTE Examples

Assume X, Y and Z are numeric fieldsX = 6, Y = 18 and Z = 5

COMPUTE Statement ResultCompute Z = Y / X + 3 Z = 6Compute Z = Y / (X + 3) Z = 2Compute Y = Z + X * 10 Y = 65Compute Y = Z * X / 10 Y = 3

7-43

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

7-44

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

7-45

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

7-46

Intrinsic Functions

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

Example

7-47

Intrinsic Functions

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

Example

7-48

Intrinsic Functions

FUNCTION function-name (argument)

• Argument - input to function– May be numeric or alpha-numeric

depending on function – Functions may have 0, 1 or more

Format

7-49

Intrinsic Functions

• Output of function - result returned after function performs its task

• Function returning alphanumeric result used in statements using alphanumeric data-items

• Function returning numeric result can be used only in arithmetic expressions

7-50

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

7-51

Chapter Summary

• COMPUTE used for any combination of arithmetic operations

• Order of evaluation of operators1. **

2. * or / in sequence left to right

3. + or - in sequence left to right

4. ( ) override normal hierarchy rules

7-52

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)

7-53

Copyright © 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.

top related