The VALUE clause in COBOL is used to assign initial values to data items in the DATA DIVISION. It provides a convenient way to set up default values without having to write explicit initialization code in the PROCEDURE DIVISION.
123401 WS-COUNTER PIC 9(3) VALUE 0. 01 WS-NAME PIC X(20) VALUE "UNKNOWN". 01 WS-INITIALIZED PIC X(5) VALUE SPACES. 01 WS-FLAG PIC X VALUE 'Y'.
The keyword IS (VALUE IS) is optional but commonly included for readability.
The VALUE clause can be used with elementary items and, in some cases, with group items. When initializing an elementary item, the value must be compatible with the item's PICTURE clause:
12301 WS-COUNT PIC 9(3) VALUE 100. 01 WS-AMOUNT PIC 9(5)V99 VALUE 123.45. 01 WS-NEGATIVE PIC S9(4) VALUE -25.
12301 WS-NAME PIC X(10) VALUE "JOHN". 01 WS-CODE PIC XXX VALUE "ABC". 01 WS-MIXED PIC X(5) VALUE "A1B2C".
The THROUGH (or THRU) keyword is used with 88-level condition names to specify a range of values. This is particularly useful for defining valid ranges for data validation.
123456701 WS-GRADE PIC 9(3). 88 PASSING-GRADE VALUE 60 THRU 100. 88 FAILING-GRADE VALUE 0 THRU 59. 01 WS-CODE PIC X. 88 VALID-LETTER VALUE "A" THRU "Z". 88 VALID-DIGIT VALUE "0" THRU "9".
The range is inclusive, meaning both endpoints are included in the valid values.
When using THRU with alphanumeric data, the comparison is based on the collating sequence of the characters. This means "A" THRU "Z" includes all uppercase letters according to the ASCII or EBCDIC collating sequence.
COBOL supports different types of literals for use with the VALUE clause:
VALUE 123
VALUE 123.45
VALUE -100
VALUE 0
or VALUE ZERO
VALUE "ABC"
or VALUE 'XYZ'
VALUE "A1B2"
VALUE "A" & rest filled with spaces
VALUE "*@#$"
12301 WS-STARS PIC X(10) VALUE ALL "*". 01 WS-ZEROES PIC 9(5) VALUE ALL "0". 01 WS-REPEATED PIC X(12) VALUE ALL "ABC".
The ALL keyword repeats the specified character or string to fill the entire data item.
88-level items (condition names) can have multiple values or value ranges, making them powerful for data validation and conditional logic.
12345678910111201 WS-STATE-CODE PIC XX. 88 EAST-COAST VALUE "NY" "NJ" "CT" "MA" "RI" "ME" "NH" "VT". 88 WEST-COAST VALUE "CA" "OR" "WA". 88 VALID-STATE VALUE "NY" "NJ" "CT" "MA" "RI" "ME" "NH" "VT" "CA" "OR" "WA" "AZ" "NV" "ID" "MT" "CO" "TX" "FL" "GA" "NC" "SC" "TN" "KY" "OH" "PA" "MI" "IL" "IN" "MO" "IA" "MN" "WI". 01 WS-RESPONSE PIC X. 88 AFFIRMATIVE VALUE "Y" "y" "1" "+". 88 NEGATIVE VALUE "N" "n" "0" "-". 88 VALID-RESPONSE VALUE "Y" "y" "N" "n" "1" "0" "+" "-".
You can combine individual values with ranges in a single VALUE clause:
1234501 WS-CHAR PIC X. 88 SPECIAL-CHAR VALUE "!" "@" "#" "$" "%" "^" "&" "*". 88 ALPHANUMERIC VALUE "A" THRU "Z" "a" THRU "z" "0" THRU "9". 88 VALID-INPUT VALUE "A" THRU "Z" "a" THRU "z" "0" THRU "9" "!" "@" "#" "$" "%" "^" "&" "*" "_" "-".
Using 88-level items with multiple values simplifies conditional logic in the PROCEDURE DIVISION:
1234567891011121314151617DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-STATE-CODE PIC XX. 88 EAST-COAST VALUE "NY" "NJ" "CT" "MA" "RI" "ME" "NH" "VT". 88 WEST-COAST VALUE "CA" "OR" "WA". PROCEDURE DIVISION. DISPLAY "Enter state code: ". ACCEPT WS-STATE-CODE. IF EAST-COAST DISPLAY "East Coast state selected" ELSE IF WEST-COAST DISPLAY "West Coast state selected" ELSE DISPLAY "Neither East nor West Coast state" END-IF.
COBOL provides several figurative constants that can be used with the VALUE clause for common initialization patterns:
ZERO, ZEROS, ZEROES
- Initialize with 0sSPACE, SPACES
- Initialize with spacesHIGH-VALUE, HIGH-VALUES
- Highest value in collating sequenceLOW-VALUE, LOW-VALUES
- Lowest value in collating sequenceQUOTE, QUOTES
- Quotation marksALL literal
- Repeated literal value1234501 WS-NAME PIC X(20) VALUE SPACES. 01 WS-COUNTER PIC 9(5) VALUE ZEROS. 01 WS-EOF-FLAG PIC X VALUE HIGH-VALUE. 01 WS-NULLS PIC X(4) VALUE LOW-VALUES. 01 WS-QUOTED PIC X(3) VALUE QUOTES.
Figurative constants automatically adjust to fill the entire size of the data item, making them versatile for initialization.
Complete the following COBOL program by adding appropriate VALUE clauses for initialization:
1234567891011121314151617181920212223242526272829303132333435363738IDENTIFICATION DIVISION. PROGRAM-ID. VALUEEX. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-RECORD. 05 EMPLOYEE-ID PIC 9(5). 05 EMPLOYEE-NAME PIC X(30). 05 EMPLOYEE-TYPE PIC X. 88 FULL-TIME _____________________. 88 PART-TIME _____________________. 88 CONTRACT _____________________. 88 VALID-TYPE _____________________. 05 SALARY-GRADE PIC 9(2). 88 ENTRY-LEVEL _____________________. 88 MID-LEVEL _____________________. 88 SENIOR-LEVEL _____________________. 05 DATE-HIRED PIC X(10). 05 STATUS-CODE PIC 9. 88 ACTIVE _____________________. 88 ON-LEAVE _____________________. 88 TERMINATED _____________________. 01 WORK-AREAS. 05 ERROR-COUNTER PIC 9(3) ____________. 05 VALID-DATA-FLAG PIC X ____________. 05 REPORT-HEADER PIC X(20) ____________. 05 SEPARATOR-LINE PIC X(50) ____________. PROCEDURE DIVISION. DISPLAY "Value Clause Exercise". IF VALID-TYPE AND ACTIVE DISPLAY "Valid and active employee" END-IF. DISPLAY SEPARATOR-LINE. STOP RUN.
Fill in the blanks with appropriate VALUE clauses.
1. What is the primary purpose of the VALUE clause in COBOL?
2. How would you initialize a field with the value "UNKNOWN"?
3. Which figurative constant represents binary zeros?
4. When using the THROUGH/THRU option with 88-level items, what does it define?
5. Which of the following is a valid VALUE clause for a numeric field?