Lesson 4

Introduction to Simple Arithmetic Operations

Introduction to Simple Arithmetic Operations

Welcome back! In the previous lesson, you explored how to define and manage numeric variables in COBOL. Understanding numeric data is essential, as many business applications rely on accurate calculations. Now, it's time to take a step further by learning about simple arithmetic operations in COBOL. This is a fundamental skill that will enable you to perform essential calculations such as addition and subtraction.

What You'll Learn

In this lesson, you will learn to perform basic arithmetic operations on numeric variables in COBOL. Specifically, we will cover:

  1. Addition and Subtraction:
    • How to add and subtract numeric variables using COBOL syntax.
    • How to display the results of these operations.

Let's delve into these topics through an example. Consider the following COBOL program that demonstrates adding and subtracting two numeric values:

cobol
1IDENTIFICATION DIVISION. 2PROGRAM-ID. AddAndSubtract. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 501 Value1 PIC 9(3). 601 Value2 PIC 9(3). 701 Result PIC 9(5). 8 9PROCEDURE DIVISION. 10 MOVE 100 TO Value1. 11 MOVE 480 TO Value2. 12 13 *> ADD operation 14 ADD Value1 TO Value2 GIVING Result. 15 DISPLAY "The result of addition is: " Result. *> 00580, same as 580 16 17 *> SUBTRACT operation 18 SUBTRACT Value1 FROM Value2 GIVING Result. 19 DISPLAY "The result of subtraction is: " Result. *> 00380, same as 380 20 21 *> SUBTRACT operation with reversed operands 22 SUBTRACT Value2 FROM Value1 GIVING Result. 23 DISPLAY "The result of subtraction is: " Result. *> 00380, same as 380 24 25 STOP RUN.

Note: When displayed, the numbers are padded with leading zeros to fill the entire field. This is a common practice in COBOL to ensure that the output is aligned correctly. However, the actual value stored in the variable does not contain these leading zeros. For simplicity, we will ignore the leading zeros in the output and focus on the actual values from now on.

Breaking Down the Code

Let's examine the code in detail to understand the arithmetic operations it performs:

  • Variable Declaration:

    cobol
    101 Value1 PIC 9(3). 201 Value2 PIC 9(3). 301 Result PIC 9(5).

    Here, Value1 and Value2 are numeric variables that can hold up to 3 digits, while Result can hold up to 5 digits.

  • Assigning Values:

    cobol
    1MOVE 100 TO Value1. 2MOVE 480 TO Value2.

    The MOVE statements assign the values 100 to Value1 and 480 to Value2.

  • Adding Values:

    cobol
    1ADD Value1 TO Value2 GIVING Result.

    This ADD statement adds Value1 and Value2, storing the result in Result. The DISPLAY statement outputs the sum:

    cobol
    1DISPLAY "The result of addition is: " Result. *> 580
  • Subtracting Values:

    cobol
    1SUBTRACT Value1 FROM Value2 GIVING Result. 2DISPLAY "The result of subtraction is: " Result. *> 380

    The SUBTRACT statement subtracts Value1 from Value2, storing the result in Result.

  • Reversed Subtraction:

    cobol
    1SUBTRACT Value2 FROM Value1 GIVING Result. 2DISPLAY "The result of subtraction is: " Result. *> 380

    The subtract operation will give the same result regardless of the order of the operands; since the variable Value1 and Value2 do not have a sign, they are unsigned integers. Hence the result for 100 - 480 and 480 - 100 will be the same 380. For now, let's just note that the order doesn't matter for unsigned integers and we'll discuss the signed integers in the upcoming lessons.

Why It Matters

Mastering addition and subtraction in COBOL is vital for developing business applications that require accurate and efficient numerical calculations. From financial transactions to data analysis, these operations form the basis of many essential tasks. By understanding how to perform arithmetic operations:

  • Enhance Functionality: Create programs that can handle complex calculations and data manipulations with confidence.
  • Increase Robustness: Ensure your applications can perform critical arithmetic operations accurately, which is crucial for maintaining data integrity in real-world scenarios.

Excited to apply these concepts? Let's dive into the practice section and solidify your understanding by writing and executing your arithmetic operations in COBOL!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.