MainframeMaster

COBOL Tutorial

COBOL VALUE OF - Quick Reference

Progress0 of 0 lessons

Overview

VALUE OF is a COBOL clause used to assign initial values to data items when they are declared. It allows you to specify default values for variables, making programs more predictable and reducing the need for explicit initialization in the PROCEDURE DIVISION.

Purpose and Usage

  • Data initialization at declaration time
  • Default value assignment
  • Predictable program behavior
  • Reduced initialization code
  • Cleaner program structure

Syntax

VALUE OF follows specific syntax patterns in data declarations:

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
* Basic VALUE OF syntax 01 counter PIC 9(4) VALUE 0. 01 status-flag PIC X VALUE "N". 01 total-amount PIC 9(8)V99 VALUE 0.00. 01 company-name PIC X(30) VALUE "ACME CORPORATION". 01 active-status PIC X VALUE "Y". * VALUE OF with different data types 01 numeric-field PIC 9(5) VALUE 12345. 01 alphabetic-field PIC A(10) VALUE "HELLO". 01 alphanumeric-field PIC X(15) VALUE "TEST DATA". 01 decimal-field PIC 9(3)V99 VALUE 123.45.

VALUE OF assigns initial values to data items at declaration time.

Group Item Initialization

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
* VALUE OF with group items 01 customer-record. 05 customer-id PIC 9(6) VALUE 0. 05 customer-name PIC X(30) VALUE SPACES. 05 customer-status PIC X VALUE "A". 05 customer-balance PIC 9(8)V99 VALUE 0.00. * VALUE OF with arrays 01 month-names. 05 month OCCURS 12 TIMES PIC X(9). 88 january VALUE "JANUARY". 88 february VALUE "FEBRUARY". 88 march VALUE "MARCH". * ... other months * VALUE OF with figurative constants 01 empty-string PIC X(50) VALUE SPACES. 01 zero-counter PIC 9(4) VALUE ZERO. 01 high-value PIC X(10) VALUE HIGH-VALUES. 01 low-value PIC X(10) VALUE LOW-VALUES.

Practical Examples

Here are some practical uses of VALUE OF in COBOL:

Counter and Accumulator 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
* Counter and accumulator initialization DATA DIVISION. WORKING-STORAGE SECTION. 01 processing-stats. 05 records-processed PIC 9(6) VALUE 0. 05 records-valid PIC 9(6) VALUE 0. 05 records-invalid PIC 9(6) VALUE 0. 05 total-amount PIC 9(10)V99 VALUE 0.00. 01 status-flags. 05 file-open PIC X VALUE "N". 05 processing-complete PIC X VALUE "N". 05 error-occurred PIC X VALUE "N". PROCEDURE DIVISION. MAIN-PROCESS. * Counters are already initialized DISPLAY "Starting processing...". DISPLAY "Initial record count: " records-processed. * Process records PERFORM UNTIL end-of-file READ input-file AT END MOVE "Y" TO end-of-file-flag NOT AT END ADD 1 TO records-processed PERFORM process-record END-READ END-PERFORM. * Display final statistics DISPLAY "Records processed: " records-processed. DISPLAY "Valid records: " records-valid. DISPLAY "Invalid records: " records-invalid. DISPLAY "Total amount: " total-amount. STOP RUN.

Counters and accumulators initialized with VALUE OF.

Configuration and Default Values

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
39
40
* Configuration and default values DATA DIVISION. WORKING-STORAGE SECTION. 01 program-config. 05 max-records PIC 9(6) VALUE 10000. 05 batch-size PIC 9(4) VALUE 100. 05 timeout-seconds PIC 9(3) VALUE 30. 05 debug-mode PIC X VALUE "N". 01 default-values. 05 default-currency PIC X(3) VALUE "USD". 05 default-country PIC X(2) VALUE "US". 05 default-language PIC X(2) VALUE "EN". 01 system-constants. 05 company-name PIC X(30) VALUE "MAINFRAME MASTERS". 05 program-version PIC X(10) VALUE "1.0.0". 05 copyright-year PIC 9(4) VALUE 2024. PROCEDURE DIVISION. INITIALIZE-PROGRAM. * Configuration is already set DISPLAY "Program: " program-version. DISPLAY "Company: " company-name. DISPLAY "Max records: " max-records. DISPLAY "Batch size: " batch-size. * Use configuration values IF debug-mode = "Y" DISPLAY "Debug mode enabled" END-IF. PERFORM process-batch. STOP RUN. PROCESS-BATCH. * Use batch size from configuration PERFORM UNTIL batch-counter >= batch-size * Process batch logic ADD 1 TO batch-counter END-PERFORM.

Configuration and default values using VALUE OF.

Best Practices

  • Use VALUE OF to initialize all data items with meaningful default values.
  • Use figurative constants like SPACES, ZERO, and HIGH-VALUES when appropriate.
  • Initialize counters and accumulators to zero or appropriate starting values.
  • Use VALUE OF for configuration data and constants.
  • Document the meaning of initial values in comments.
  • Consider the impact of initialization on program performance.

Common Pitfalls

  • Not initializing data items, leading to unpredictable behavior.
  • Using inappropriate initial values for the data type.
  • Forgetting to initialize counters and accumulators.
  • Using VALUE OF with incompatible data types.
  • Not considering the impact of initialization on memory usage.

Test Your Knowledge

1. What is VALUE OF in COBOL?

  • A data type
  • A value assignment clause
  • A file operation
  • A program structure

2. Where is VALUE OF typically used?

  • In the PROCEDURE DIVISION
  • In the DATA DIVISION
  • In the ENVIRONMENT DIVISION
  • In the WORKING-STORAGE SECTION

3. What is the primary purpose of VALUE OF?

  • To perform calculations
  • To assign initial values to data items
  • To read files
  • To display output

4. Can VALUE OF be used with all data types?

  • Yes, always
  • No, only with numeric data
  • No, only with alphanumeric data
  • It depends on the COBOL implementation

5. When is the VALUE OF assignment made?

  • At runtime
  • At compile time
  • When the program starts
  • When the data item is first used

Frequently Asked Questions