Lesson 2
Simple Calculations in Group Items
Building on Your Knowledge

Welcome back to our journey through enhanced data management in COBOL! In the previous lesson, we explored how group items can streamline your programs by organizing related data together. Now, let's take that knowledge a step further. This lesson will introduce you to simple calculations within group items. Even though these calculations are straightforward, they play a crucial role in managing and manipulating data effectively.

What You'll Learn

In this lesson, you will learn how to perform arithmetic operations on group items. Specifically, you will:

  1. Define numeric fields within a group item.
  2. Move values to these fields.
  3. Perform simple arithmetic calculations using the ADD statement.

Here’s a sneak peek at what you’ll be able to write by the end of this lesson:

cobol
1IDENTIFICATION DIVISION. 2PROGRAM-ID. GroupCalcDemo. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 501 Account. 6 05 Account-Balance PIC 9(6) VALUE 0. 7 05 Deposit PIC 9(6) VALUE 0. 8 05 New-Balance PIC 9(6) VALUE 0. 9 10PROCEDURE DIVISION. 11 MOVE 5000 TO Account-Balance. 12 MOVE 1500 TO Deposit. 13 14 DISPLAY Account-Balance. *> 5000 15 DISPLAY Deposit. *> 1500 16 17 ADD Account-Balance TO Deposit GIVING New-Balance. 18 DISPLAY "New Account Balance: " New-Balance OF Account. *> 6500 19 20 STOP RUN.
Practical Applications

Arithmetic operations are fundamental in nearly every program you write. Whether it's calculating account balances like in our example or figuring out inventory levels in a retail system, these operations are essential in everyday coding tasks.

Consider a scenario at a bank where you need to update a customer’s account balance after a deposit. Using group items and arithmetic operations, you can quickly and efficiently compute the new balance. This not only makes your code cleaner but also far more efficient.

In this example, we have an Account group item that includes fields for Account-Balance, Deposit, and New-Balance. Simple operations like moving values and adding them provide the backbone for more complex financial calculations you might encounter in real-world applications.

Why It Matters

Understanding how to perform simple calculations within group items enriches your COBOL programming toolkit. It enables you to manage numerical data efficiently and paves the way for advanced arithmetic operations. Mastery of these fundamental skills will give you confidence in tackling more complex scenarios in your coding career.

Excited? I hope so! Let's move on to the practice section and solidify these skills together.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.