TExcelTAD
Home › COBOL › COBOL VALUE & Constants

COBOL VALUE & Constants

The VALUE clause gives a data item its initial value when the program starts. It is not an assignment statement: it only describes the starting content. To change a value later you use MOVE, arithmetic verbs or INITIALIZE in the PROCEDURE DIVISION.

Literals

Kind Examples Notes
Numeric literal 42, -7, 3.14, +0.5 Up to 38 digits, optional sign and decimal point
Alphanumeric literal "Hello", 'Hello' Single or double quotes; double the quote to include one: "Say ""hi"""
Hexadecimal literal X"41" Bytes given in hex (X"41" is the letter A in ASCII)
cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. LITERALS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-COUNT       PIC 9(3)       VALUE 42.
       01  WS-RATE        PIC S9V999     VALUE -0.125.
       01  WS-QUOTE       PIC X(20)      VALUE "She said ""yes""".
       01  WS-LETTERS     PIC X(3)       VALUE X"414243".
       PROCEDURE DIVISION.
           DISPLAY WS-COUNT
           DISPLAY WS-RATE
           DISPLAY WS-QUOTE
           DISPLAY WS-LETTERS
           STOP RUN.
042
-0.125
She said "yes"
ABC

Figurative constants

COBOL has named constants that fill an item of any size:

Constant Meaning
ZERO, ZEROS, ZEROES The digit 0 (fills a text field with 0 characters)
SPACE, SPACES Space characters
HIGH-VALUE, HIGH-VALUES The highest character value (X"FF") — often used as an end-of-file sentinel
LOW-VALUE, LOW-VALUES The lowest character value (X"00")
QUOTE, QUOTES The quote character
ALL literal The literal repeated to fill the item
cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. FIGURATIVE.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-TOTAL       PIC 9(5)       VALUE ZERO.
       01  WS-NAME        PIC X(6)       VALUE SPACES.
       01  WS-RULE        PIC X(20)      VALUE ALL "-".
       01  WS-STARS       PIC X(9)       VALUE ALL "*+".
       01  WS-TEXT-ZEROS  PIC X(4)       VALUE ZEROS.
       PROCEDURE DIVISION.
           DISPLAY "[" WS-TOTAL "]"
           DISPLAY "[" WS-NAME "]"
           DISPLAY WS-RULE
           DISPLAY WS-STARS
           DISPLAY "[" WS-TEXT-ZEROS "]"
           MOVE ALL "=" TO WS-RULE
           DISPLAY WS-RULE
           STOP RUN.
[00000]
[      ]
--------------------
*+*+*+*+*
[0000]
====================

Figurative constants also work in MOVE and in comparisons: IF WS-NAME = SPACES.

VALUE on groups

A VALUE on a group item must be text and initialises the whole group. More often, each child gets its own VALUE. A classic pattern is a heading line built from FILLER items with fixed text:

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. HEADING.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-HEADING.
           05  FILLER         PIC X(10)  VALUE "ITEM".
           05  FILLER         PIC X(6)   VALUE "QTY".
           05  FILLER         PIC X(8)   VALUE "PRICE".
       01  WS-PAIR            VALUE "AB12".
           05  WS-PAIR-TEXT   PIC XX.
           05  WS-PAIR-NUM    PIC 99.
       PROCEDURE DIVISION.
           DISPLAY WS-HEADING
           DISPLAY WS-PAIR-TEXT " / " WS-PAIR-NUM
           STOP RUN.
ITEM      QTY   PRICE
AB / 12

INITIALIZE

INITIALIZE resets a group: numeric children become zero and alphanumeric children become spaces. FILLER items are left alone, which is handy for print lines. With REPLACING you choose other values.

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. INIT-DEMO.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  WS-INVOICE.
           05  INV-NUMBER     PIC 9(4)   VALUE 1001.
           05  INV-CUSTOMER   PIC X(8)   VALUE "Globex".
           05  INV-TOTAL      PIC 9(4)V99 VALUE 99.5.
       PROCEDURE DIVISION.
           DISPLAY "Before: [" WS-INVOICE "]"
           INITIALIZE WS-INVOICE
           DISPLAY "After:  [" WS-INVOICE "]"
           INITIALIZE WS-INVOICE REPLACING ALPHANUMERIC BY "?"
           DISPLAY "Again:  [" WS-INVOICE "]"
           STOP RUN.
Before: [1001Globex  009950]
After:  [0000        000000]
Again:  [0000?       000000]

The REPLACING value is moved like any other MOVE, so "?" padded to 8 characters became ? plus seven spaces. The numeric fields were still reset to zero.

Named constants

COBOL 2002 added true constants with CONSTANT AS. They cannot be changed and can be used where a literal is expected (GnuCOBOL also accepts the older 78 level for the same purpose):

cobol · try it yourself
       IDENTIFICATION DIVISION.
       PROGRAM-ID. CONSTANTS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  MAX-ITEMS          CONSTANT AS 50.
       01  SHOP-NAME          CONSTANT AS "TAD Market".
       01  WS-ORDERED         PIC 99 VALUE 12.
       01  WS-LEFT            PIC 99.
       PROCEDURE DIVISION.
           SUBTRACT WS-ORDERED FROM MAX-ITEMS GIVING WS-LEFT
           DISPLAY SHOP-NAME ": " WS-LEFT " of " MAX-ITEMS " left"
           STOP RUN.
TAD Market: 38 of 50 left

Test yourself

What does VALUE ALL "-" do on a PIC X(20) item?

  1. Stores one hyphen and 19 spaces
  2. Fills all 20 positions with hyphens
  3. Causes an error
  4. Stores the text ALL -

After INITIALIZE, what do numeric and alphanumeric children contain?

  1. Both spaces
  2. Both zeros
  3. Zeros and spaces respectively
  4. Their original VALUE clauses

How do you write the text It's as a single-quoted literal?

  1. 'It\'s'
  2. 'It''s'
  3. "It's" only
  4. It is impossible