Constant
What is the Constant?
A constant is a variable assigned with a value that does not change throughout the program execution. COBOL does not have any system-defined standards for constants except figurative constants.
The programmer can define a variable with an initial value using the VALUE clause during the variable declaration. If the variable value doesn't change during the program's execution, the variable is considered as a constant variable, and the value is considered as a constant value.
Syntax -
level-number constant-variable
PIC data-type-character(constant-length)
VALUE constant-value.
For example - Declaring a variable to store a value 3.14 (PI value).
01 WS-PI PIC 9(2)V9(2) VALUE 3.14.
- level-number - Specifies the level number of the declaration from 01 to 49. From example, it is 01.
- constant-variable - Specifies the name of the constant. From example, it is WS-PI.
- data-type-character - Specifies the type of the variable. From example, it is 9.
- constant-length - Specifies the variable length to store the data. From example, it is 04.
- constant-value - Specifies the constant value. From example, it is 3.14.
Types -
Constants are three types and those are –
- Numeric constants – Numeric variables having one value throughout the program execution are called numeric constants. For example - 01 WS-PI PIC 9(2)V9(2) VALUE 3.14.
- Alphanumeric constants/non-numeric constants – Alphanumeric variables that have only one value throughout the program execution are called as alphanumeric constants. They require literals enclosed with quotes while initializing them. For example - 01 WS-HI PIC X(05) VALUE "HI".
- Figurative Constants - System-defined constants are predefined in the COBOL language and used as replacements for standard values like spaces, zeroes, etc. For example - 01 WS-VAR PIC 9(5) VALUE ZEROES.
For example -
01 A PIC X(10) VALUE "MAINFRAMES".
01 B.
02 C PIC 9(3) VALUE 255.
02 D PIC 9(3) VALUE ZEROES.
In the above example, A is a non-numeric constant variable, C is a numeric constant variable, and D is a figurative constant variable.
Practical Example -
Scenario - Example to describe how the constants are used in COBOL programming.
Code -
----+----1----+----2----+----3----+----4----+----5----+
IDENTIFICATION DIVISION.
PROGRAM-ID. CONSTANT.
AUTHOR. MTH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-NUMCONST PIC 9(10) VALUE 256.
05 WS-NNUMCONST PIC X(05) VALUE "TRUE".
05 WS-FIGCONST PIC X(10) VALUE ZEROES.
05 WS-VAR1 PIC 9(10).
05 WS-VAR2 PIC X(20).
PROCEDURE DIVISION.
MOVE 256 TO WS-VAR1.
IF WS-VAR1 EQUAL WS-NUMCONST
DISPLAY "WS-VAR1 VALUE EQUAL TO NUMERIC CONSTANT"
ELSE
DISPLAY "WS-VAR1 VALUE NOT EQUAL TO NUMERIC CONSTANT"
END-IF.
MOVE "FALSE" TO WS-VAR2.
IF WS-VAR2 EQUAL WS-NNUMCONST
DISPLAY "WS-VAR2 VALUE EQUAL TO NON-NUMERIC CONSTANT"
ELSE
DISPLAY "WS-VAR2 VALUE NOT EQUAL TO NON-NUMERIC CONSTANT"
END-IF.
IF WS-FIGCONST EQUAL ZEROES
DISPLAY "WS-FIGCONST IS FILLED WITH ZEROES"
DISPLAY "FIGCONSTANT : " WS-FIGCONST
END-IF.
STOP RUN.
Output -
WS-VAR1 VALUE EQUAL TO NUMERIC CONSTANT WS-VAR2 VALUE NOT EQUAL TO NON-NUMERIC CONSTANT WS-FIGCONST IS FILLED WITH ZEROES FIGCONSTANT : 0000000000
Explaining Example -
In the above example:
- We have declared three constant variables. i.e., WS-NUMCONST is a numeric constant with a constant value of 256.
- WS-NNUMCONST is a non-numeric constant with a constant value of "TRUE".
- Finally, WS-FIGCONST is a variable initialized with the figurative constant ZEROES.