Data Types & Variables :--
COBOL supports variables which are either alphabetic, numeric, or alphanumeric.
Variable declaration
Variables must be declared in a COBOL program prior to their use. Declarations occur in the DATA DIVISION. Data items are either "elementary data items" or "record description entries."
Elementary items
DATA DIVISION.
WORKING-STORAGE SECTION.
77 HOURS-WORKED PICTURE 99 IS ZERO.
A record is a group of items which contain related data values. For example, we might define a record as follows:
The 77 above is referred to as a level number. Level 77 is specific level number used to describe elementary items in the working storage section of the data division.
Note that this data item also has included an optional initialization value. Values may be either literal or figurative. Literal values are written as follows. Numeric 1-18 digits
Option: may be preceeded by + or - sign
Option: may include an embedded decimal point
Alphabetic 0 or more characters enclosed in quotes (").
Option: to include a quote in a literal string,type two
Figurative values are:
Figurative values are:
- ZERO
- ZEROES
- ZEROS
- SPACE
- SPACES
- ALL "string"
the ALL modifier is used to fill an alphabetic variable with copies of the string. For
example,
77 OUTPUT-STRING PICTURE X(21) IS ALL "+-".
would be the same as
77 OUTPUT-STRING PICTURE X(21) IS "+-+-+-+-+-+-+-+-+-+-+".
Group items
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ALBUM.
05 TITLE PICTURE X(30).
05 GENRE PICTURE X(10).
05 ARTIST.
10 FIRST-NAME PICTURE X(20).
10 LAST-NAME PICTURE X(20).
10 BAND-NAME PICTURE X(20).
05 ID-NUMBER PICTURE X(10).
05 YEAR PICTURE 9999.
The level numbers above are used to show subordination of groups of values. Level 01 is the uppermost level in the hierarchy. Other numbers can be chosen as the programmers preference in the range of 02-49. All items with the same level number are at the same hierarchical level in the record, referenced through the data name that subordinates them.
In other words, this data description shows a group called ALBUM with the five subordinate items, TITLE, GENRE, ID-NUMBER and YEAR (all elementary) and another group called ARTIST with three elementary items, FIRST-NAME, LAST-NAME, and BAND-NAME.
Note: all user-defined names at the 01-level must be unique; however, it is permissible to use the same subordinate names in other group variables.
Group items can be referenced in a program as an entire unit, for example,
ALBUM
or by the items within the structure. For example, we may have,
TITLE OF ALBUM
or
BAND-NAME OF ARTIST OF ALBUM
to refer to specific data items.
Boolean data items
COBOL does not directly support logical/boolean variables; however, level-88 is used to define condition names which have the same effect. For example, suppose we have a variable called CLASS-YEAR which is a number in the range 0-5 with the following interpretation:
0 a student who has been accepted but has not yet registered for courses
1 a student in the freshman year
2 sophomore
3 junior
4 senior
5 graduate
77 CLASS-YEAR PIC 9.
88 NON-MATRICULATED VALUE 0.
88 FROSH VALUE 1.
88 SOPH VALUE 2.
88 JUNIOR VALUE 3.
88 SENIOR VALUE 4.
88 GRADUATE VALUE 5.
88 INVALID-CLASS VALUES 6,7,8,9.
which would allow statements like
IF GRADUATE ...
whose result would be determined by the value currently stored in CLASS-YEAR when this statement is pocessed. Note that it is possible to have more than one value associated with a condition. For example, above, if any number outside 0 through 5 is stored in CLASS-YEAR the data name INVALID-CLASS will become true.