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.
VALUE OF follows specific syntax patterns in data declarations:
123456789101112* 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.
1234567891011121314151617181920* 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.
Here are some practical uses of VALUE OF in COBOL:
12345678910111213141516171819202122232425262728293031323334353637* 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.
12345678910111213141516171819202122232425262728293031323334353637383940* 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.
1. What is VALUE OF in COBOL?
2. Where is VALUE OF typically used?
3. What is the primary purpose of VALUE OF?
4. Can VALUE OF be used with all data types?
5. When is the VALUE OF assignment made?