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:
PIC S9
.Let's take a look at an example to see how it's done:
cobol1IDENTIFICATION 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:
S
indicates the variable is signed, allowing for both positive and negative values.S9(3)
) specifies how many digits the number can have, excluding the sign.Example:
cobol101 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:
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!