What’s new at MAINFRAMES 360
(Updates May '11)...
- We are a cosmopolitan community of 400 members, from around the globe.
- Working on writing and publishing articles on CICS.
- Special article on "How do I learn Mainframe Programming"

Saturday, September 26, 2009

COBOL Tutorial – Arithmetic Operators


Q. How do you perform Arithmetic in COBOL? For example, how do you write a simple program that calculates the Interest to be paid on a Principal p?
You often need to do Math in COBOL. You can perform Arithmetic in COBOL using Arithmetic Operators. When you perform some arithmetic on two or more numeric quantities, such an expression is called an Arithmetic Expressions. There are 4 main Arithmetic Operators in COBOL – ADD, SUBTRACT, MULTIPLY and DIVIDE.

You can even write complex arithmetic expressions by using the COMPUTE verb. I have written about the syntax of each operator, followed by examples and some results.
Q. How’z the ADD Operator used in COBOL. Show me some stuff...

The ADD operator can be used to add two or more quantities. In the assembly language, we write the instruction ADD A B to find the sum of A and B. The final sum is stored in A. On the same lines, the ADD Operator in COBOL has the following syntax :

Syntax

ADD one-quantity TO second-quantity

The final sum result is stored in the second-variable. For example, if A=10, and I write

ADD 5 TO A

After performing the above step, the computer adds 5 to the value of A, so A=15. Suppose a variable B=5, then I may also write

ADD B TO A

Syntax

ADD A B C TO SUM

In this case, all the three group of numbers A B and C, would be added to SUM. So, assuming A=10, B=15, and C=20, if initially RESULT = 100, after addition of A, B and C to SUM, the value of RESULT = 100 + (10+15+20) = 145.

Example 

Image12[1] 

Result 

Image13[1]

Syntax

ADD A B GIVING RESULT

If you would like the final result of arithmetic to be stored in a separate resultant variable, you can always write a GIVING clause at the end. Thus, if A=5 and B=10, the sum(addition of A and B), would yield 15. This sum 15 is stored in the RESULT Variable. On omitting the GIVING Clause, the sum is stored in one of the operands being added. GIVING Clause allows you to specify a separate destination(receiving variable) which shall hold the final result of computation.

Example

Image14[2]

Result

Image15[1]

Syntax

ADD A B C GIVING D E

The GIVING Clause may also specify a list of destination/receiving variables. In that case, sum(addition) is copied to the each of the variables mentioned in the GIVING Clause. For example,
Assume that A = 10, B = 15 and C=20.

Now the sum of A B C = 10 + 15 + 20 = 45. The sum 45 is stored in each of the variables mentioned in the list(D, E). Thus,
D = 45 and E = 45, after the above statement is executed.

Example :


Image16[2]

Result :

Image17[1]

Comparing GIVING Clause with TO Clause :

Suppose RESULT = 100.

ADD 10 20 GIVING RESULT. 
After the operation, RESULT = 30 (Old value is overwritten with the new value)

ADD 10 20 TO RESULT.
After the operation, RESULT = 130 (Old value is merely added to the operands, to arrive at the new value)

Q. How does the SUBTRACT Operator work in COBOL?

When you want to find the difference of two or more quantities in COBOL, you use the SUBTRACT operator. The SUBTRACT operator subtracts one quantity from another, and finds out the result. In its simplest form, SUBTRACT has the following syntax :

Syntax

SUBTRACT one-quantity FROM second-quantity

For example,

SUBTRACT WS-VAR-2 FROM WS-VAR-1

Consider the variable WS-VAR-1 = 10, WS-VAR-2=30, subtracting WS-VAR-2 from WS-VAR-1 would result in the difference between 30 and 10. The answer is 20. This final result is stored in WS-VAR-2. SUBTRACT B FROM A is equivalent to the statement A = A – B.

Image03[2] 

The example above is shown in the code snippet below :

Image04[2]

The output of the above code snippet would look like this :

Image05[1]
However, at times, you would like to store the difference between these two quantities in a separate third variable. To do so, you would put the following syntax to use :

Syntax

SUBTRACT variable-1 FROM variable-2 GIVING variable-3

For example, you would write SUBTRACT WS-VAR-1 FROM WS-VAR-2 GIVING WS-VAR-3, with WS-VAR-1=30, WS-VAR-2=10, you get WS-VAR-3 = WS-VAR-2 minus WS-VAR-1, i.e. WS-VAR-3 = 30 – 10 = 20.

Consider the code snippet given below :

Image08[1] 

Upon running the above code snippet, you would get the following output :

Image09[2] 

COBOL also gives you the facility to subtract a group of numbers from a given quantity. For example, let’s assume that you want to subtract a group of numbers 4 5 1 from a given sum 25. You compute the result as follows :
25 - (4 + 5 + 1) = 15.
In COBOL you would write it as,

SUBTRACT 4 5 1 FROM 25

Syntax

SUBTRACT item-1 item-2 item-3  ... FROM SUM [GIVING OUTPUT]

The example below shows, how to use this syntax in COBOL

Image10[2]

The output of the above code-snippet, when you would run, should look like this -

Image11[3] 

Q. How does MULTIPLY Verb compute the product of two numbers in COBOL?

The MULTIPLY Verb is used to multiply two quantities together, that results in a Product. Let us assume the following data-variables, that’ll help us to understand the example.

Image18[1]

The general form of MULTIPLY Verb is :

Syntax:

MULTIPLY number-1 BY number-2

This simple statement, multiplies the number-2 by number-1. For example, if A = 5, MULTIPLY 2 BY A, would result in A getting doubled, so the new value of A=10.

Image19[1]

Result

Image22[3]

Syntax:

MULTIPLY number-1 BY number-2 GIVING result

As you know, the GIVING Clause specifies a separate destination or receiving variable. So, the product is stored in the GIVING Clause variable result. For example, if A = 10, B = 20, MULTIPLY A BY B GIVING C, results in
10 x 20 = 200 getting stored in C.

Example

Image20[1]


Result

Image%2025[1]
Syntax:

MULTIPLY A BY B C D 

This format of MULTIPLY Statement specifies that, A is to be multiplied each of the variables B, C and D, so that it results in the products A x B, A x C, A x D. The final resulting three products are stored in B, C and D itselves. If A = 10, and B = 2, C = 3 and D = 4, after the executing this MULTIPLY instruction, B, C and D become ten-folds, B = 20, C=30 and D=40.

Example

Image26[2]

Result

Image27[2]

Syntax:


MULTIPLY A BY B C GIVING D E 

In this form of the MULTIPLY statement, as you would guess there would be 2 products : A x B (A times B) and
A x C(A times C). These final products would be stored in separate receiving variables D and E, as specified by the GIVING Clause.

Q. How does DIVIDE Verb divide a one operand by another? How do you find the remainder(what’s left over)?

The DIVIDE verb in COBOL, is used compute the quotient, upon the division of two numbers. The DIVIDE Verb has several forms available.

DIVIDE A INTO B

Here, the DIVIDE Operator is used to perform simple Division. You divide B into ‘A’ equal parts, how many parts do you get? In other words, this statement calculates the fraction B/A. Assume that you have a chocolate that’s 100  length. If you were to divide it into 5 cm long bars, you would get 100/5 = 20 bars to eat. To do this you would write,

DIVIDE 5 INTO 100

DIVIDE A INTO B C

You may also divide the multiple dividends(operands) by the same divisor, using a single statement. Suppose A=2, and B=58,C=156. Then, DIVIDE A INTO B C would mean that you are trying to divide 2 into 58 and 156. So, both 58 and 156 would be halved to 29 and 78.

DIVIDE A INTO B C GIVING D E

GIVING Clause can be used optionally to specify separate receiving/destination variables for storing the results.

DIVIDE B BY A GIVING C

You can also use the BY Clause to specify the dividend first and then the divisor.

Wednesday, September 2, 2009

REDEFINES Clause, RENAMES Clause


Contents

  1. REDEFINES Clause
    1. Simple Program to show how REDEFINES Works
  2. RENAMES Clause
    1. Simple Program to show how RENAMES Clause works


Q. What is the REDEFINES Clause in COBOL? Why is it used?

REDFINES Clause can be used to define two group data-items which point to the same storage area. You can use both the data-items in the same COBOL Program.

Suppose, we have a storage area defined in the Working Storage section as

01 EMPLOYEE-DETAILS.
   02 EMP-NAME PIC X(30).
   02 EMP-NAME-DETAILS REDEFINES EMP-NAME.
      03 EMP-FNAME PIC X(10).
      03 EMP-MNAME PIC X(10).
      03 EMP-LNAME PIC X(10).
 
   02 EMP-ADDRESS PIC X(26).
   02 EMP-ADDRESS-DETAILS REDEFINES EMP-ADDRESS.
      03 EMP-STREET PIC X(10).
      03 EMP-CITY PIC X(10).
      03 EMP-PINCODE PIC X(6).
   02 EMP-CONTACT PIC X(20).

   02 EMP-CONTACT-DETAILS REDEFINES EMP-CONTACT.
      03 EMP-PHONE1 PIC 9(10).
      03 EMP-PHONE2 PIC 9(10).

Note that :
1) In the above example, EMP-NAME has been redefined as EMP-NAME-DETAILS, EMP-ADDRESS is redefined as EMP-ADDRESS-DETAILS and EMP-CONTACT is redefined as
EMP-CONTACT-DETAILS. So, EMP-NAME occupies 30 bytes of storage area. The same 30-byte storage area is also pointed to by EMP-NAME-DETAILS, and it provides a breakup into FNAME, MNAME and LNAME. This is a common and typical use of REDEFINES clause.

2) One cannot use the REDFINES clause with 01-level(root-level) data-items in the FILE SECTION. It also should not be used 66-level(renames clause) and 88-level(condition names).

3) The data-item which you want to redefine should not have a OCCURS Clause.

4) Note that, the original data-items and redefined ones both refer to the same memory location in storage area. Hence, in the above example, the total space occupied by EMPLOYEE-DETAILS still remains the same = 30+26+20 = 76.

Q. Show me a COBOL Program that demonstrates how to use the REDEFINES Clause.

Given below is a COBOL Program that shows a practical use of REDEFINES.



 

Upon running the above COBOL Program LOAD, we get the following output in the SYSOUT Dataset -


Q. What is the RENAMES Clause? Is it a good practice to use the RENAMES Clause?

The RENAMES clause is used to regroup data-items. The RENAMES Clause entry always must have level no. 66. You can take existing Data-items, and re-group them, and create a new copy in the memory under a new data-name. Note, that both the copies – the memory locations always remain in sync. While using the RENAMES clause, one must follow certain rules in COBOL. They are as follows :

1) RENAMES clause has the following syntax

    66 new-data-name RENAMES data-item-1 THRU
data-item-2
2) The 66 level RENAMES entry should immediately follow data-items 1 and 2.

3) The Data-Items 1 and 2 should not be 01-Level record entries.

4) The Data-Items 1 and 2 should be contiguous.

5) The new-data name should be one logical level higher than the data-item-1 and data-item-2.

6) The use of RENAMES Clause has often been debated in COBOL, and its considered a malpractice to use the RENAMES Clause now-a-days. Instead, one must opt for REDEFINES Clause.

Q. Show me a simple working example of the RENAMES Clause..

Given below is a basic COBOL Program, that shows how to use the RENAMES Clause.





In the above example, there are two copies of FIELD-B, one in record-1 and another in record-3. Note that both copies of FIELD-B will always contain the same value/contents. Any changes to FIELD-B will affect record-1 as well as record-3. Note that, unlike REDEFINES clause, RENAMES clause occupies actual physical storage space. Upon submitting the above COBOL Program LOAD, we get the following output in the SYSOUT Dataset.


Related Posts Plugin for WordPress, Blogger...

Note :

Protected by Copyscape Online Copyright Protection
© Copyright – Quasar Chunawalla, 2010.
Note : The copyrights of all the material, text and pictures posted in this website belong to the author. Any instance of lifting the material from this website, shall be considered as an act of plagiarism. For any clarifications, please drop me a line at
 
back to top