MainframeMaster

COBOL Tutorial

VALUE Clause in COBOL

Progress0 of 0 lessons

VALUE Clause for Initialization

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.

Basic VALUE Clause Syntax

cobol
1
2
3
4
01 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:

Numeric Values

cobol
1
2
3
01 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.

Alphanumeric Values

cobol
1
2
3
01 WS-NAME PIC X(10) VALUE "JOHN". 01 WS-CODE PIC XXX VALUE "ABC". 01 WS-MIXED PIC X(5) VALUE "A1B2C".

THROUGH/THRU for Ranges

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.

Using Value Ranges

cobol
1
2
3
4
5
6
7
01 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.

Literal Types (Numeric, Alphanumeric)

COBOL supports different types of literals for use with the VALUE clause:

Numeric Literals

  • Integer literals: VALUE 123
  • Decimal literals: VALUE 123.45
  • Signed literals: VALUE -100
  • Zero: VALUE 0 or VALUE ZERO

Alphanumeric Literals

  • Quoted strings: VALUE "ABC" or VALUE 'XYZ'
  • Mixed content: VALUE "A1B2"
  • Space filled: VALUE "A" & rest filled with spaces
  • Special characters: VALUE "*@#$"

Using the ALL Keyword

cobol
1
2
3
01 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.

Multiple Values for 88-Level Items

88-level items (condition names) can have multiple values or value ranges, making them powerful for data validation and conditional logic.

Multiple Values Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
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". 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:

Combining Values and Ranges

cobol
1
2
3
4
5
01 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:

Using Multiple Values in Conditions

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
DATA 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.

Figurative Constants (SPACES, ZEROS, etc.)

COBOL provides several figurative constants that can be used with the VALUE clause for common initialization patterns:

Common Figurative Constants

  • ZERO, ZEROS, ZEROES - Initialize with 0s
  • SPACE, SPACES - Initialize with spaces
  • HIGH-VALUE, HIGH-VALUES - Highest value in collating sequence
  • LOW-VALUE, LOW-VALUES - Lowest value in collating sequence
  • QUOTE, QUOTES - Quotation marks
  • ALL literal - Repeated literal value

Examples

cobol
1
2
3
4
5
01 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.

Cautions with Figurative Constants

  • HIGH-VALUES and LOW-VALUES depend on the character set (ASCII or EBCDIC)
  • On EBCDIC systems, LOW-VALUES is binary zeros, but not on ASCII systems
  • Use caution when moving between character sets or platforms
  • For numeric data items, use ZERO or ZEROS rather than SPACES

Exercise: Working with VALUE Clauses

Complete the following COBOL program by adding appropriate VALUE clauses for initialization:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
IDENTIFICATION 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.

Knowledge Check

Test Your Knowledge

1. What is the primary purpose of the VALUE clause in COBOL?

  • To define the data type of a field
  • To initialize data items with specific values
  • To calculate values at runtime
  • To restrict the size of a field

2. How would you initialize a field with the value "UNKNOWN"?

  • VALUE IS "UNKNOWN"
  • VALUE IS UNKNOWN
  • VALUE "UNKNOWN"
  • VALUE = "UNKNOWN"

3. Which figurative constant represents binary zeros?

  • ZEROES
  • NULLS
  • BINARY-ZEROS
  • LOW-VALUES

4. When using the THROUGH/THRU option with 88-level items, what does it define?

  • A recursive initialization pattern
  • A range of acceptable values
  • A linked list of values
  • An array of values

5. Which of the following is a valid VALUE clause for a numeric field?

  • VALUE IS "123"
  • VALUE IS 123
  • VALUE IS ZEROES
  • All of the above

Frequently Asked Questions