VALUE and figurative constants
A VALUE clause gives a WORKING-STORAGE item its starting value when the
program is loaded:
01 WS-STATUS PIC X(6) VALUE "ACTIVE".
01 WS-PAGE-NO PIC 9(3) VALUE 1.
The value must suit the picture: text for X, a number for 9. An item
with no VALUE has no guaranteed starting content. GnuCOBOL happens to
set it to spaces or zeros, but on z/OS it may contain whatever was in
memory, depending on compiler options. Give every counter and total a
VALUE; don't rely on luck.
Figurative constants
COBOL has reserved words for common values. They fill the whole field, however long it is:
| Constant | Fills the field with |
|---|---|
ZERO, ZEROS, ZEROES |
the digit 0 |
SPACE, SPACES |
spaces |
HIGH-VALUE, HIGH-VALUES |
the highest possible byte value (hex FF) |
LOW-VALUE, LOW-VALUES |
the lowest possible byte value (hex 00) |
QUOTE, QUOTES |
the " character |
ALL "literal" |
the literal, repeated |
Singular and plural forms mean exactly the same thing. You can use them
in a VALUE clause or as the source of a MOVE:
IDENTIFICATION DIVISION.
PROGRAM-ID. FIGS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-RULE PIC X(20) VALUE ALL "-".
01 WS-TOTAL PIC 9(5) VALUE ZEROS.
01 WS-NAME PIC X(8) VALUE SPACES.
01 WS-STATUS PIC X(6) VALUE "ACTIVE".
PROCEDURE DIVISION.
DISPLAY WS-RULE.
DISPLAY "TOTAL [" WS-TOTAL "] NAME [" WS-NAME "]".
MOVE ZEROS TO WS-STATUS.
DISPLAY "STATUS [" WS-STATUS "]".
DISPLAY WS-RULE.
STOP RUN.
--------------------
TOTAL [00000] NAME [ ]
STATUS [000000]
--------------------
Note the third line: ZEROS into an alphanumeric field fills it with
the character 0. Figurative constants don't care about the field's
type.
HIGH-VALUES and LOW-VALUES
These can't be printed meaningfully, but they are everywhere in batch
code. HIGH-VALUES is greater than any real data, so when a program
reaches the end of an input file it often moves HIGH-VALUES to that
file's key. Any comparison against real keys then falls the right way, and
the matching logic finishes the other file with no special cases. You will
use this pattern when you process sorted files.
IDENTIFICATION DIVISION.
PROGRAM-ID. HIVAL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-LAST-KEY PIC X(6) VALUE HIGH-VALUES.
PROCEDURE DIVISION.
IF WS-LAST-KEY = HIGH-VALUES
DISPLAY "NO MORE INPUT"
END-IF.
STOP RUN.
This prints NO MORE INPUT. (IF is covered properly in a later module.)
Resetting a record
To clear a whole record between customers, MOVE SPACES to the group.
Careful: a group is alphanumeric, so its numeric fields get spaces too,
which is not a valid number. INITIALIZE group-name is safer: it sets
alphanumeric fields to spaces and numeric fields to zeros in one
statement.
On the job
A classic abend (S0C7, "data exception") happens when a program does
arithmetic on a numeric field that contains spaces. It is often caused
by a missing VALUE ZEROS or a MOVE SPACES to a group containing
numeric fields.
Your task
An order-entry program must clear the customer fields after each order.
- Give the fields starting values:
-
WS-RULE: thirty=characters, usingALL-WS-STATUS:"OPEN"-WS-NAME:SPACES-WS-QTY:ZEROS - After the
BEFOREline, resetWS-NAMEto spaces andWS-QTYto zeros usingMOVEwith figurative constants.
For the input ANNA and 150 the output must be:
==============================
BEFORE: [ANNA ] [0150] [OPEN ]
AFTER: [ ] [0000] [OPEN ]
==============================