VALUE Clause
- VALUE clause assigns a value to a variable at the time of declaration.
- This initialization happens before the first statement execution in the PROCEDURE DIVISION.
- It's a way to ensure that specific variables have initialized values when a program begins its execution.
Rules -
- It should only code with PIC clause.
- If the variable doesn't have a VALUE clause coded with it, the value of the variable is unpredictable.
- Values coded should not exceed the length of the PIC clause.
- It should not be coded with REDEFINES, JUSTIFIED, or SYNCHRONIZED clauses.
Formats -
VALUE clause has two different formats -
- Initializing variables
- Declaring condition names
Initializing variables -
This format is used to initialize a variable during its declaration.
Syntax -
---+----2----+----3----+----4----+----5
level-number data-name-1 PIC ... [VALUE literal-1].
Parameters -
- PIC clause - Defines the type and size of the variable.
- literal-1 - Refers to the value we wish to assign to the variable and should be compatible with the PIC clause.
Examples -
Scenario1 - Alphanumeric Initialization.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(15) VALUE 'Mainframes'.
Here, the alphanumeric variable WS-NAME is initialized with the value 'Mainframes'.
Scenario2 - Numeric Initialization.
WORKING-STORAGE SECTION.
01 WS-AGE PIC 9(02) VALUE 25.
Here, the numeric variable WS-AGE starts with a default value of 25.
Declaring condition names -
This format declares a condition name with a value, or multiple values, or a range of values.
Syntax -
---+----2----+----3----+----4----+----5
88 data-name-1 [VALUE [IS|ARE] literal-1 [THRU literal2].
Parameters -
- literal-1 - Refers to the value we wish to assign to the variable. If THRU is coded, this is the starting value.
- THRU literal-2 - Refers to the ending value we wish to assign to the variable.
Examples -
Scenario1 - Single value Initialization.
WORKING-STORAGE SECTION.
01 WS-GENDER PIC X(01).
88 MALE VALUE 'M'.
88 FEMALE VALUE 'F'.
Here, MALE and FEMALE are condition names assigned with single value.
Scenario2 - Multiple values Initialization.
WORKING-STORAGE SECTION.
01 WS-GENDER PIC X(01).
88 VALID-GENDER VALUE 'M' 'F'.
Here, VALID-GENDER is a condition name assigned with multiple values 'M' and 'F'.
Scenario3 - Range of values Initialization.
WORKING-STORAGE SECTION.
01 WS-MARKS PIC 9(03).
88 FIRST-CLASS VALUE 60 THROUGH 100.
Here, FIRST-CLASS is a condition name assigned with range of values from 60 to 100.