Lesson 2

Hello again! In our previous lesson, we explored the different types of string variables in **COBOL**. Now, we will switch gears and dive into numeric data types, specifically focusing on *signed integers* and how **COBOL** manages negative values. This is an important skill that will be useful when dealing with real-world calculations and data manipulations in your **COBOL** programs.

In this lesson, you will learn how to:

- Define signed integer variables using
`PIC S9`

. - Perform basic arithmetic operations (addition, subtraction, multiplication, and division) with signed integers.
- Display and interpret the results of these operations, especially when negative values are involved.

Let's take a look at an example to see how it's done:

cobol`1IDENTIFICATION DIVISION. 2PROGRAM-ID. SignedIntegerNumbers. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 5 601 Num1 PIC S9(3) VALUE 100. 701 Num2 PIC S9(3) VALUE -456. 801 Result PIC S9(5). 9 10PROCEDURE DIVISION. 11 ADD Num1 TO Num2 GIVING Result. 12 DISPLAY Result. *> -356 13 14 SUBTRACT Num1 FROM Num2 GIVING Result. 15 DISPLAY Result. *> -556 16 17 MULTIPLY Num1 BY Num2 GIVING Result. 18 DISPLAY Result. *> -45600 19 20 DIVIDE Num2 BY Num1 GIVING Result. 21 DISPLAY Result. *> -4 (integer division) 22 23 STOP RUN.`

When defining signed integer variables in **COBOL**, here are the key components:

**PIC S9**: The`S`

indicates the variable is*signed*, allowing for both positive and negative values.**Number of Digits**: The number in parentheses (e.g.,`S9(3)`

) specifies how many digits the number can have, excluding the sign.

Example:

cobol`101 Num1 PIC S9(3) VALUE 100. 201 Num2 PIC S9(3) VALUE -456. 301 Result PIC S9(5).`

In this example:

`Num1`

and`Num2`

can hold values from -999 to 999.`Result`

can hold up to 5 digits, excluding the sign. Note that we have a signed number type for the Result variable, as it is going to hold signed numbers.

Understanding *signed integers* and how to work with them is crucial for many aspects of software development. Whether you are calculating balances, updating inventories, or processing financial transactions, you'll need to handle both positive and negative numbers.

By mastering signed integers in **COBOL**, you will be better equipped to:

- Perform complex mathematical computations accurately.
- Detect and handle erroneous conditions in your programs more effectively.
- Write more meaningful and robust applications that can deal with real-world scenarios involving negative values.

These skills are vital for creating reliable and efficient software solutions.

Excited to get started? Let’s dive into the practice section and solidify your understanding of signed integers in **COBOL**. Happy coding!