Summary -

In this topic, we described about the Constant with detailed example.

A constant is a variable/data item that has only one value throughout the program execution.

COBOL does not have any defined standards for constants except figurative constants.

However, the programmer can define a variable with an initial value using the VALUE clause during the variable declaration.

The values provided after the VALUE clause are referred to as constants.

Constants are three types and those are –

  • Numeric constants – Constants having only numeric values and do not require any quotes to declare them.
    For example - 123, 657, 33443, 256478 etc,.
  • Alphanumeric constants/non-numeric constants – Constants having non-numeric, or alphabetic, or alphanumeric values. They require quotes while declaring them.
    For example - "HELLO" "TRUE" "1" 'HI' '123' etc,.
  • Figurative Constants - Constants with values that are predefined in the COBOL language and used as replacements for standard values like spaces, zeroes, etc.
    For example - ZERO, ZEROS, ZEROES, SPACE, SPACES, HIGH-VALUE, HIGH-VALUES etc,.

Let’s take the below example to understand better.

01 A		PIC X(10) VALUE "MAINFRAMES".
01 B.
   02 C		PIC 9(3) VALUE 255.
   02 FILLER	PIC 9(3) VALUE ZEROES.

In the above example, MAINFRAMES is a non-numeric constant, 256 is a numeric constant, and ZEROES is a figurative constant.

Figurative constants -

Figurative constants are the constant values that are predefined in the COBOL language.

These figurative constants are known to the COBOL language and perform the specific task if specified.

Below are the figurative constants in COBOL language -

Figurative constant Description
ZERO, ZEROS, ZEROES Specifies the numeric value ZERO(0) or one/more occurrences of the ZERO depending on the context.
For example - 01 WS-VAR   PIC 9(5) VALUE ZEROES.
SPACE, SPACES Represents one or more blanks or spaces.
For example - 01 WS-VAR   PIC X(5) VALUE SPACES.
HIGH-VALUE, HIGH-VALUES Represents one or more occurrences of the character with the highest position in the used assembling sequence. i.e., the value X'FF' is used.
For example - 01 WS-VAR   PIC X(5) VALUE HIGH-VALUES.
LOW-VALUE, LOW-VALUES Represents one or more occurrences of the character with the lowest position in the used assembling sequence. i.e., the value X'00' is used.
For example - 01 WS-VAR   PIC X(5) VALUE LOW-VALUES.
QUOTE, QUOTES Represents one or more occurrences of -
  • The quotation mark character ("), if the QUOTE compiler option is in effect.
  • The apostrophe character ('), if the APOST compiler option is in effect.
For example - 01 WS-VAR   PIC X(10) VALUE QUOTES.
ALL Represents one or more occurrences of the figurative constant.
ALL literal represents one or more occurrences of the figurative constant that compose the literal.
The figurative constant should follow ALL.
For example - 01 WS-VAR   PIC X(10) VALUE ALL QUOTES.
symbolic-character Specifies one or more symbolic-characters defined in the SYMBOLIC CHARACTERS clause of the SPECIAL-NAMES paragraph.
NULL, NULLS Represents a value used while defining USAGE POINTER, USAGE PROCEDURE-POINTER, USAGE FUNCTION-POINTER, USAGE OBJECT REFERENCE, or the ADDRESS OF special register do not contain a valid address.
For example - 01 WS-VAR-PTR   PIC S9(04) VALUE NULL.

Usage -

Figurative-constants used in many ways in the COBOL program. Those are specified below -

  • Initialization in DATA DIVISION
    For example -
    01 WS-VAR.
    	05 WS-VAR1    PIC 9(05) VALUE ZEROES.
    	05 WS-VAR2    PIC X(05) VALUE SPACES.
    	05 WS-VAR3    PIC X(10) VALUE HIGH-VALUES.
  • Moving to fields.
    For example -
    MOVE ZEROES TO WS-VAR1.
    MOVE SPACES TO WS-VAR2.
    MOVE HIGH-VALUES TO WS-VAR3.
  • Filling the entire field.
    For example -
    MOVE ALL ZEROES TO WS-VAR1.
    MOVE ALL SPACES TO WS-VAR2.
    MOVE ALL HIGH-VALUES TO WS-VAR3.
  • Comparing the contents of the fields.
    For example -
    IF WS-VAR1 EQUAL ZEROES
    .....
    IF WS-VAR2 EQUAL SPACES
    .....
    IF WS-VAR3 EQUAL HIGH-VALUES

Example -

Scenario - The below example describes how the constants are used in COBOL programming.

Code -

Constant program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7-
***************************** Top of Data ******************************
       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.                                                    
**************************** Bottom of Data ****************************

Output -

Constant program Output

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 "TRUE". Finally, WS-FIGCONST is a variable initialized with figurative constant ZEROES.

The COBOL program was written to explain the processing of constant variables. In the above example, hard-coded values are assigned to the variables for the reader's understanding. However, the values might be set in different ways like from other programs or communication areas, etc.