Working Storage

When you define a variable in WORKING-STORAGE, you also can assign it an initial value. This is a convenient method of setting variables to start with a known value.

Variables are initialized with a VALUE IS clause, as shown in lines 000900 and 001000 of the Sample Code. Note that the period closing the variable definition is at the end of the initialize, so the sequence is the level number, the variable name, PICTURE IS (or PIC), the picture, VALUE IS, the initializer, and finally the period.

Initializing a variable in WORKING-STORAGE has the same effect as a MOVE to the variable. If the initializing value is shorter than the PICTURE of an alphanumeric field, the field is padded on the right with spaces.

If the initializing value is too small for a numeric variable, the variable is padded on the left with zeroes.

The definition for the variable, THE-MESSAGE, is at lines 000800 and 000900.   The definition is broken up into two lines. The 01 level starts in Area A, but only the level number of the variable is required to start in Area A.

The remainder of the definition (the variable name, picture, and value) falls within Area B (columns 12 through 72).

The initializer for THE-MESSAGE--in this case, "Jack be nimble"--is clearly too short for the PICTURE, and the remainder of THE-MESSAGE is filled with spaces by the compiler when it encounters the VALUE clause. Similarly, THE-NUMBER is initialized with a 1, and the compiler fills the variable space with 01 when it encounters the VALUE IS clause.

Note that initializing a variable with a VALUE in WORKING-STORAGE is the same as using MOVE to give it a value. Thereafter, you can use MOVE to assign values to the variable later in the program. THE-MESSAGE, initialized at lines 000800 and 000900, is modified by a MOVE at line 002500 and again later at line 003300.
Initializing a variable in WORKING-STORAGE
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. JACK04.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500
000600 WORKING-STORAGE SECTION.
000700
000800 01  THE-MESSAGE      PIC X(50).
000900 01  THE-NUMBER       PIC 9(2) VALUE IS 1.
001000 01  A-SPACE          PIC X    VALUE IS " ".
001100
001200 PROCEDURE DIVISION.
001300 PROGRAM-BEGIN.
001400
001500* Set up and display line 1
001600     MOVE "Jack be nimble," TO THE-MESSAGE.
001700     DISPLAY
001800         THE-NUMBER
001900         A-SPACE
002000         THE-MESSAGE.
002100
002200* Set up and Display line 2
002300     ADD 1 TO THE-NUMBER.
002400     MOVE "Jack be quick," TO THE-MESSAGE.
002500     DISPLAY
002600         THE-NUMBER
002700         A-SPACE
002800         THE-MESSAGE.
002900
003000* Set up and display line 3
003100     ADD 1 TO THE-NUMBER.
003200     MOVE "Jack jump over the candlestick." TO THE-MESSAGE.
003300     DISPLAY
003400         THE-NUMBER
003500         A-SPACE
003600         THE-MESSAGE.
003700
003800 PROGRAM-DONE.
003900     STOP RUN.
004000
004100