Module lessons (2/2)
Variables and PICTURE
In COBOL, all variables used must be declared inside the DATA DIVISION. Specifically, temporary variables in memory used for program logic reside inside the WORKING-STORAGE SECTION.
Level Numbers
COBOL variable declaration makes use of level numbers to structure and organize data hierarchically:
01: Defines a main variable or group item (the highest level).05,10,15: Define subordinate fields within a level01group item.77: Historically indicated independent elementary variables that could not be subdivided further.
In modern practice, level 01 is commonly used to declare independent elementary variables.
The PICTURE (or PIC) Clause
The PICTURE (abbreviated PIC) clause specifies the type of data and the character length of the variable. The three fundamental format characters are:
X(Alphanumeric): Can contain letters, numbers, and special characters.PIC X(10)declares a string of exactly 10 characters.
9(Numeric): Can only contain numeric digits.PIC 9(3)declares an integer composed of 3 digits (up to 999).
V(Implicit Decimal Point): Used in decimal numbers to represent where the fractional part begins (the decimal point is not stored physically).PIC 9(3)V99declares a number with 3 integer digits and 2 decimal places.
The VALUE Clause
To assign an initial value to a variable at declaration time, use the VALUE clause.
01 WS-PROJECT-NAME PIC X(15) VALUE "ELEARNER".
01 WS-USER-AGE PIC 9(3) VALUE 25.
All variable definitions must end with a period ..
Try it yourself
Declare a level 01 alphanumeric variable named WS-USER-NAME of length 20 characters and set its initial value to 'ALICE'.
Show hint
Use the format: 01 WS-USER-NAME PIC X(20) VALUE 'ALICE'.
Solution available after 3 attempts
Declare a level 01 numeric variable named WS-USER-AGE of 3 digits with an initial value of 25.
Show hint
Use the syntax: 01 WS-USER-AGE PIC 9(3) VALUE 25.
Solution available after 3 attempts