Cob Basics 4

26

description

Cob Basics 4

Transcript of Cob Basics 4

Page 1: Cob Basics 4
Page 2: Cob Basics 4

TITLE : Data handling in COBOL

Purpose:• To introduce how to do manipulations with data in COBOL• To introduce how to move data between different variables

OBJECTIVES : • How to handle arithmetic operations in COBOL• How to move data between variables• How to use Intrinsic functions of COBOL• How to handle strings in COBOL

Unit4

Page 3: Cob Basics 4

Arithmetic Verbs

• The following are the arithmetic verbs in COBOL– ADD– SUBTRACT– MULTIPLY– DIVIDE– Compute

Page 4: Cob Basics 4

Add Verb

• The ADD statement sums two or more numeric operands and stores the result.– ADD {identifier-1} {literal-1}...TO identifier-2... – ADD {identifier-1} {literal-1}...TO {identifier-2}{literal-

2} GIVING identifier-3...• All identifiers or literals preceding the key word TO are added

together, and this sum is added to and stored in identifier-2.• The values of the operands preceding the word GIVING are

added together, and the sum is stored as the new value of each data item referenced by identifier-3.

• The fields or operands that are added should be numeric

Page 5: Cob Basics 4

Subtract Verb

• The SUBTRACT statement subtracts one numeric item, or the sum of two or more numeric items, from one or more numeric items, and stores the result.

• Format 1– SUBTRACT {identifier-1} {literal-1} .. FROM identifier-2 ..

• Format 2– SUBTRACT {identifier-1} {literal-1} .. FROM {identifier-2}

{literal-2} GIVING identifier-3...• All identifiers or literals preceding the key word FROM are added

together and this sum is subtracted from and stored immediately in identifier-2.

• All identifiers or literals preceding the key word FROM are added together and this sum is subtracted from identifier-2 or literal-2. The result of the subtraction is stored as the new value of each data item referenced by identifier-3.

Page 6: Cob Basics 4

Multiply Verb

• The MULTIPLY statement multiplies numeric items and sets the values of data items equal to the results.

• FORMAT 1:--MULTIPLY {identifier-1} {literal-1} BY identifier-2..

• FORMAT 2:--MULTIPLY {identifier-1} {literal-1} BY {identifier-2} {literal-2} GIVING identifier-3...

• In format 1, the value of identifier-1 or literal-1 is multiplied by the value of identifier-2; the product is then placed in identifier-2.

• In format 2, the value of identifier-1 or literal-1 is multiplied by the value of identifier-2 or literal-2. The product is then stored in the data item (s) referencedby identifier-3.

Page 7: Cob Basics 4

Divide Verb

• Format 1 DIVIDE {identifier-1} {literal-1} INTO identifier-2 • Format 2 DIVIDE {identifier-1} {literal-1} INTO {identifier-

2} {literal-2} GIVING identifier-3...• Format 3 DIVIDE {identifier-1} {literal-1} BY {identifier-2}

{literal-2} GIVING identifier-3...• Format 4 DIVIDE {identifier-1} {literal-1} INTO {identifier-

2} {literal-2{ GIVING identifier-3 REMAINDER identifier-4..• Format 5 DIVIDE {identifier-1} {literal-1} BY {identifier-2}

{literal-2} GIVING identifier-3 REMAINDER identifier-4..• Either the word INTO or BY may be used with a DIVIDE

statement. • The GIVING clause is optional with INTO but required with

BY.

Page 8: Cob Basics 4

Divide Verb• In format 1, the value of identifier-1 or literal-1 is divided into the

value of identifier-2, and the quotient is then stored in identifier-2.• In format 2, the value of identifier-1 or literal-1 is divided into the

value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3.

• In format 3, the value of identifier-1 or literal-1 is divided by the value of identifier-2 or literal-2. The value of the quotient is stored in each data item referenced by identifier-3.

• In format 4, the value of identifier-1 or literal-1 is divided into identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4.

• In format 5, the value of identifier-1 or literal-1 is divided by identifier-2 or literal-2. The value of the quotient is stored in identifier-3, and the value of the remainder is stored in identifier-4.

Page 9: Cob Basics 4

Rounded Option

• ROUNDED is optional with all arithmetic operations.

• If the ROUNDED option is not specified, truncation of decimal positions will occur if the resultant field cannot accommodate all the decimal positions in the answer.

• With the ROUNDED option, the computer will always round the result to the PICTURE specification of the receiving field.

• If ROUNDED and REMAINDER are to be used in the same DIVIDE statement, ROUNDED must appear first.

Page 10: Cob Basics 4

On Size Error Option

• Avoiding Size Errors– The best way to avoid a size error condition is to be

absolutely certain that the receiving field is large enough to accommodate any possible result.

• COBOL has a built-in solution to the problem if the error occurs. Use an ON SIZE ERROR clause with any arithmetic operation as follows:

• FORMAT: arithmetic statement [ON SIZE ERROR

imperative statement . . . ]

Page 11: Cob Basics 4

Compute Verb

• The COMPUTE statement assigns the value of an arithmetic expression to one or more data items.

• COMPUTE identifier-1 [ROUNDED] . . . = {arithmetic expression-1} {literal- 1}{identifier-2} [ON SIZE ERROR imperative statement]

• On most systems, you must follow precise spacing rules when using the COMPUTE statement. That is, the equal sign as well as the arithmetic symbols must be preceded and followed by a space.

Page 12: Cob Basics 4

Compute Verb

• **• * or / (whichever appears 1st from left to right)• + or - (whichever appears 1st from left to right)• The use of parentheses overrides rules 1--3. That is, operations

within parentheses are performed first.• Without parentheses, exponentiation is performed first. • Multiplication and division operations follow any

exponentiation and precede addition or subtraction operations. • If there are two or more multiplication or division operations,

they are evaluated from left to right in the expression. • Addition and subtraction are evaluated last, also--left to right

Page 13: Cob Basics 4

Arithmetic Operations• When performing arithmetic, you must make certain that the receiving

field is large enough to accommodate the result.

• Addition--the resultant field should be one position larger than the largest field being added.

• Subtraction--resultant field should be as large as the field being subtracted from.

• Multiplication--resultant field size should equal the sum of the lengths of the operands being multiplied.

• Division--resultant field size should equal the sum of the number of digits in the divisor and dividend.

Page 14: Cob Basics 4

Arithmetic Operations

• Product is + if multiplicand and multiplier have the same sign. • Product is - if multiplicand and multiplier have different signs.

• Quotient is + if dividend and divisor have the same sign.• Quotient is - if dividend and divisor have different signs.

• If signs of the fields being added are the same, add uses the sign.• If signs of the fields being added are different

add all + numbers, and add all - numbers separately. Then subtract the smaller total from the larger total and use the sign of the larger.

Page 15: Cob Basics 4

Move Verb

• The MOVE statement transfers data from one area of storage to one or more other areas.

• Format 1MOVE identifier-1 TO identifier-2

• PICTURE clauses of both fields must be the same.

• A second form of the MOVE statement is as follows:Format 2

MOVE literal-1 TO identifier -2

Page 16: Cob Basics 4

Move VerbRULE 1: MOVING INTEGER PORTIONS OF NUMERIC FIELDS• When moving an integer sending field or an integer portion of a

numeric sending field, to a numeric receiving field, movement is from right to left.

• All non filled high-order (leftmost) integer positions of the receiving field are replaced with zeros.

RULE 2: MOVING DECIMAL PORTIONS OF NUMERIC FIELDS• When moving a decimal portion of a numeric sending field to the

decimal portion of a numeric receiving field, movement is from left to right, beginning at the implied decimal point.

• Low-order (rightmost) non filled decimal positions of the receiving field are replaced with zeros.

Page 17: Cob Basics 4

Move Verb

• Numeric literals are moved to fields in exactly the same manner as numeric fields are moved.

If a numeric field have negative contents, then it must have an S in its PIC clause.

If we code MOVE - 123 TO AMT1, for example, then AMT1 should be defined with a PIC S9(3).

An S should be included in the PIC clause of a numeric field whenever the sign of the number is to be retained.

Page 18: Cob Basics 4

Move Verb

• In a nonnumeric move, data is transmitted from the sending field to the receiving field from left to right.

• Low-order or rightmost positions of the receiving field that are not replaced with sending field characters are filled with spaces.

• A Group Move Is Considered a Nonnumeric Move• All group items, even those with numeric sub fields, are

treated as alphanumeric fields.

Page 19: Cob Basics 4

Move Verb

• Example01 A.

05 B PIC X(4) VALUE ‘ABCD’. 05 C PIC 9(5) VALUE 12345.

01 D. 05 E PIC X(3). 05 F PIC 9(2). 05 G PIC X(4). MOVE A TO D.E : ABC, F : D1, G : 2345

Page 20: Cob Basics 4

Move Verb

• If the same name is used to define fields in different records or group items, indicate which record or group item is to be accessed by qualifying the identifier with the word OF or IN.

• When more than one field in storage has the same name, we qualify the name in the PROCEDURE DIVISION as follows:

• Format– Identifier-1 OF record-name-1 IN group-item-name-1

Page 21: Cob Basics 4

String Verb

Page 22: Cob Basics 4

String Verb

• The STRING statement strings together the partial or complete contents of two or more data items or literals into one single data item.

• STRING Ident1, Ident2, "10" DELIMITED BY SIZE INTO DestString

• The STRING statement moves characters from the source string to the destination string according to the rules for alphanumeric to alphanumeric moves.

• The ON OVERFLOW clause executes if there are still valid characters left in the source strings but the destination string is full.

Page 23: Cob Basics 4

String Verb

• Data movement from a particular source string ends when either;– the end of the source string is reached – the end of the destination string is reached – the delimiter is detected.

• The WITH POINTER phrase allows an identifier/dataname to be kept which holds the position in the Destination String where the next character will go.

• The DELIMITED BY SIZE clause means that the whole of the sending field will be added to the destination string

Page 24: Cob Basics 4

Unstring Verb

Page 25: Cob Basics 4

Unstring Verb

• The UNSTRING statement causes contiguous data in a sending field to be separated and placed into multiple receiving fields.

• Characters are copied from the source string to the destination strings according to the rules for alphanumeric moves

• If the DELIMITED BY phrase is not specified, the DELIMITER IN and COUNT IN phrases must not be specified.

• The UNSTRING statement terminates when either; – All the characters in the source string have been examined – All the destination strings have been processed – Some error condition is encountered (such as the pointer

pointing outside the source string).

Page 26: Cob Basics 4

Unstring Verb

• The COUNT IN clause is associated with a particular destination string and holds a count of the number of characters passed to the destination string.

• Only one TALLYING clause can be used with each UNSTRING. It holds a count of the number of destination strings affected by the UNSTRING operation.

• When the ALL phrase is used, contiguous delimiters are treated as if only one delimiter had been encountered.

• If the ALL is not used, contiguous delimiters will result in spaces being sent to some of the destination strings