Welcome back! In the previous lesson, you learned about defining and working with alphabetic variables in COBOL. Now, it's time to move on to another important topic: handling numbers. Understanding how to manage numeric data in COBOL is crucial, as many business applications require precise calculations and data manipulations.
In this lesson, you will learn how to define and use numeric variables in COBOL. Specifically, you will cover the following:
Numeric Variables:
Using the DISPLAY
Statement:
Let's take a look at an example to see these concepts in action:
cobol1IDENTIFICATION DIVISION. 2PROGRAM-ID. NumericVariables. 3DATA DIVISION. 4WORKING-STORAGE SECTION. 501 Account-Number PIC 9(10). *> Number with 10 digits 601 Short-Account-Number PIC 9(5) VALUE 1234. *> Number with 5 digits with initial value 1234 7PROCEDURE DIVISION. 8 MOVE 1234567890 TO Account-Number. 9 DISPLAY "Account Number: " Account-Number. *> Will display 1234567890 10 11 DISPLAY "Short Account: " Short-Account-Number. *> Will display 01234 as the value is 1234 12 MOVE 12345 TO Short-Account-Number. 13 DISPLAY "Short Account: " Short-Account-Number. *> Will display 12345 14 MOVE 123456 TO Short-Account-Number. 15 DISPLAY "Short Account: " Short-Account-Number. *> Will display 23456 16 STOP RUN.
In COBOL, numeric variables are used to store and manipulate numerical data. They are defined using the PIC
clause followed by a specification that indicates the type and size of the number.
For example:
cobol101 Account-Number PIC 9(10). *> Number with 10 digits 201 Short-Account-Number PIC 9(5) VALUE 1234. *> Number with 5 digits with initial value 1234
01 Account-Number PIC 9(10).
declares a numeric variable named Account-Number
that can store up to 10 digits.01 Short-Account-Number PIC 9(5) VALUE 1234.
declares a numeric variable named Short-Account-Number
that can store up to 5 digits and is initially set to 1234
.Notice how the PIC
clause is used to define the variable type and size. In this context, 9
indicates a numeric digit placeholder. When initializing variables, as seen with Short-Account-Number
, you can use the VALUE
keyword.
Just like with alphanumeric variables, you can use the MOVE
and DISPLAY
statements to work with numeric variables.
For example:
cobol1MOVE 1234567890 TO Account-Number.
2DISPLAY "Account Number: " Account-Number.
3DISPLAY "Short Account: " Short-Account-Number.
4MOVE 12345 TO Short-Account-Number.
5DISPLAY "Short Account: " Short-Account-Number.
6MOVE 123456 TO Short-Account-Number.
7DISPLAY "Short Account: " Short-Account-Number.
MOVE
assigns values to the numeric variables.DISPLAY
shows the values of the numeric variables on the screen.When the above code is executed, you'll see:
Plain text1Account Number: 1234567890 2Short Account: 01234 3Short Account: 12345 4Short Account: 23456
Note that Short-Account-Number
initially displays 01234
because it pads the initial value of 1234
with a zero. Later, when the value 123456
is moved to Short-Account-Number
, only the last five digits 23456
are displayed.
Understanding and effectively managing numeric data is vital for processing and manipulating numerical information in business applications. From financial transactions to inventory management, numeric calculations are a core function in COBOL programs. By mastering numeric variables, you'll be better equipped to:
Excited to see how numbers work in COBOL? Let's dive into the coding practice and enhance your skills further!