VALUE Clause
The VALUE clause is used to assign an initial value to a variable at the time of declaration. This initialization happens when the program starts executing and before any other operation occurs. It's a way to ensure that specific variables have default values when a program begins its execution.
Rules -
- If the variable is not initialized, the value of the variable is unpredictable.
- Values provided should not exceed the length of the PICTURE clause.
- It should not be coded with REDEFINES, JUSTIFIED, or SYNCHRONIZED clauses.
- It should only code with PIC clause.
Formats -
VALUE clause has two different formats to initialize the items -
- For Variables
- For Condition names
For Variables -
This format is used to assign the initial value for a variable.
Syntax -
---+----2----+----3----+----4----+----5
level-number data-name-1 PIC ... [VALUE literal-1].
Parameters -
- PIC clause - It defines the type and size of the data item.
- literal-1 - This is the actual value we want to assign to the data item. It 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.
For condition names -
This format initializes the condition name with a value, multiple values, or a range of values. Each condition name requires a separate 88 level entry.
Syntax -
---+----2----+----3----+----4----+----5
88 data-name-1 [VALUE [IS|ARE] literal-1 [THRU literal2].
Parameters -
- literal-1 - This is the actual value we want to assign to the data item. If THRU is coded, this is the starting value.
- THRU literal-2 - This is the ending value we want to assign to the data item. The keywords THROUGH and THRU are equivalent.
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.