In this section, we will cover the basics of performing arithmetic operations in COBOL. Arithmetic operations are fundamental in any programming language, and COBOL provides a straightforward way to handle them. We will discuss the following topics:
- Basic Arithmetic Operations
- Arithmetic Verbs
- Order of Operations
- Practical Examples
- Exercises
- Basic Arithmetic Operations
COBOL supports the following basic arithmetic operations:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
)
These operations can be performed using arithmetic verbs or inline expressions.
- Arithmetic Verbs
COBOL provides specific verbs for arithmetic operations. These verbs are:
ADD
SUBTRACT
MULTIPLY
DIVIDE
Syntax and Examples
ADD
The ADD
verb is used to add numbers.
This statement adds the value of A
to B
and stores the result in B
.
SUBTRACT
The SUBTRACT
verb is used to subtract numbers.
This statement subtracts the value of A
from B
and stores the result in B
.
MULTIPLY
The MULTIPLY
verb is used to multiply numbers.
This statement multiplies the value of A
by B
and stores the result in B
.
DIVIDE
The DIVIDE
verb is used to divide numbers.
This statement divides the value of B
by A
and stores the result in B
.
- Order of Operations
COBOL follows the standard mathematical order of operations (PEMDAS/BODMAS):
- Parentheses
- Exponents (not directly supported in COBOL)
- Multiplication and Division (left to right)
- Addition and Subtraction (left to right)
When using arithmetic verbs, the order of operations is determined by the sequence of the statements.
- Practical Examples
Let's look at some practical examples to understand how arithmetic operations are performed in COBOL.
Example 1: Addition and Subtraction
IDENTIFICATION DIVISION. PROGRAM-ID. ArithmeticExample1. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9(2) VALUE 10. 01 B PIC 9(2) VALUE 20. 01 C PIC 9(2). PROCEDURE DIVISION. ADD A TO B GIVING C. DISPLAY 'Result of A + B: ' C. SUBTRACT A FROM B GIVING C. DISPLAY 'Result of B - A: ' C. STOP RUN.
Example 2: Multiplication and Division
IDENTIFICATION DIVISION. PROGRAM-ID. ArithmeticExample2. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9(2) VALUE 10. 01 B PIC 9(2) VALUE 20. 01 C PIC 9(2). PROCEDURE DIVISION. MULTIPLY A BY B GIVING C. DISPLAY 'Result of A * B: ' C. DIVIDE B BY A GIVING C. DISPLAY 'Result of B / A: ' C. STOP RUN.
- Exercises
Exercise 1: Basic Arithmetic Operations
Write a COBOL program to perform the following operations:
- Add 15 and 25.
- Subtract 10 from 50.
- Multiply 6 by 7.
- Divide 100 by 5.
Solution
IDENTIFICATION DIVISION. PROGRAM-ID. ArithmeticExercise1. DATA DIVISION. WORKING-STORAGE SECTION. 01 A PIC 9(2) VALUE 15. 01 B PIC 9(2) VALUE 25. 01 C PIC 9(2) VALUE 10. 01 D PIC 9(2) VALUE 50. 01 E PIC 9(2) VALUE 6. 01 F PIC 9(2) VALUE 7. 01 G PIC 9(3) VALUE 100. 01 H PIC 9(2) VALUE 5. 01 RESULT PIC 9(3). PROCEDURE DIVISION. ADD A TO B GIVING RESULT. DISPLAY '15 + 25 = ' RESULT. SUBTRACT C FROM D GIVING RESULT. DISPLAY '50 - 10 = ' RESULT. MULTIPLY E BY F GIVING RESULT. DISPLAY '6 * 7 = ' RESULT. DIVIDE G BY H GIVING RESULT. DISPLAY '100 / 5 = ' RESULT. STOP RUN.
Common Mistakes and Tips
- Incorrect Data Types: Ensure that the data types of the variables are appropriate for the operations being performed.
- Order of Operations: Be mindful of the order in which operations are performed, especially when using multiple arithmetic verbs.
- Division by Zero: Always check for division by zero to avoid runtime errors.
Conclusion
In this section, we covered the basics of arithmetic operations in COBOL, including the use of arithmetic verbs and the order of operations. We also provided practical examples and exercises to reinforce the concepts. Understanding these fundamentals is crucial for performing calculations and data manipulation in COBOL programs. In the next section, we will delve into control structures, which will allow you to make decisions and repeat actions in your COBOL programs.