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:

  1. Basic Arithmetic Operations
  2. Arithmetic Verbs
  3. Order of Operations
  4. Practical Examples
  5. Exercises

  1. 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.

  1. 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.

ADD A TO B.

This statement adds the value of A to B and stores the result in B.

SUBTRACT

The SUBTRACT verb is used to subtract numbers.

SUBTRACT A FROM B.

This statement subtracts the value of A from B and stores the result in B.

MULTIPLY

The MULTIPLY verb is used to multiply numbers.

MULTIPLY A BY B.

This statement multiplies the value of A by B and stores the result in B.

DIVIDE

The DIVIDE verb is used to divide numbers.

DIVIDE A INTO B.

This statement divides the value of B by A and stores the result in B.

  1. Order of Operations

COBOL follows the standard mathematical order of operations (PEMDAS/BODMAS):

  1. Parentheses
  2. Exponents (not directly supported in COBOL)
  3. Multiplication and Division (left to right)
  4. Addition and Subtraction (left to right)

When using arithmetic verbs, the order of operations is determined by the sequence of the statements.

  1. 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.

  1. Exercises

Exercise 1: Basic Arithmetic Operations

Write a COBOL program to perform the following operations:

  1. Add 15 and 25.
  2. Subtract 10 from 50.
  3. Multiply 6 by 7.
  4. 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.

© Copyright 2024. All rights reserved