Level numbers 01 to 49, 77 and 88
Every entry in the data division begins with a level number, and the number says where the entry sits in the tree. Two entries with the same number are siblings, a higher number belongs to the entry above it, and a lower number closes the group. The tree is what a record is.
Levels make a hierarchy
Levels 01 to 49 describe the two kinds of entry. A group item has a name and no picture, and the entries after it with higher numbers are its children. An elementary item has a picture and no children, and it is the only kind that takes bytes.
The numbers only have to rise: 01, 05, 10 is a record of three levels, and 01, 05, 05, 10 is the same record with two children and a grandchild. Nothing counts the gaps.
IDENTIFICATION DIVISION.
PROGRAM-ID. LV.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER.
05 CUSTOMER-ID PIC 9(4) VALUE 101.
05 CUSTOMER-NAME PIC X(10) VALUE "ADA".
05 CUSTOMER-ADDRESS.
10 STREET PIC X(12) VALUE "MAIN STREET".
10 CITY PIC X(8) VALUE "LONDON".
77 COUNTER-FIELD PIC 9(3) VALUE 7.
PROCEDURE DIVISION.
DISPLAY "ID: " CUSTOMER-ID.
DISPLAY "STREET:" STREET.
DISPLAY "CITY: " CITY.
DISPLAY "COUNT: " COUNTER-FIELD.
DISPLAY "CUSTOMER is " LENGTH OF CUSTOMER " bytes".
DISPLAY "ADDRESS is " LENGTH OF CUSTOMER-ADDRESS " bytes".
DISPLAY "COUNTER is " LENGTH OF COUNTER-FIELD " bytes".
STOP RUN.
cobc -x -free -Wall -o lv lv.cob
./lv
ID: 0101
STREET:MAIN STREET
CITY: LONDON
COUNT: 007
CUSTOMER is 34 bytes
ADDRESS is 20 bytes
COUNTER is 3 bytes
STREET and CITY can be used by name in the procedure division even though they are two levels down: a name is a name, and the tree is for the addresses, not for the access.
How long a group is
A group takes no bytes of its own; it is the fields under it, in the order they are declared. The last three lines of the output say so, and the numbers add up by hand:
The record is 4 + 10 + 20 bytes, the address inside it is 12 + 8, and the level 77 item is 3. A record with an OCCURS or a REDEFINES in it does not add up this simply, and the lessons on OCCURS and REDEFINES come back to the sum.
Level 77
A level 77 item is a field of its own, with no parent and no children: it is not part of any record. It takes bytes like any elementary item, and it cannot be a group, so a level below it is refused:
IDENTIFICATION DIVISION.
PROGRAM-ID. L77.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 COUNTER-FIELD PIC 9(3).
05 PART PIC 9.
PROCEDURE DIVISION.
DISPLAY COUNTER-FIELD.
STOP RUN.
cobc -x -free -Wall -o l77 l77.cob
l77.cob:7: error: level number must begin with 01 or 77
5 | WORKING-STORAGE SECTION.
6 | 77 COUNTER-FIELD PIC 9(3).
7 > 05 PART PIC 9.
8 |
9 | PROCEDURE DIVISION.
The message is about the line after the 77, and it means that a data division cannot start here: after a 77 the next entry has to be a new 77 or a new 01.
Level 88
A level 88 is not storage at all. It is a condition name attached to the field above it, and it gives a value of that field a name. An IF may then ask the name instead of the value, and SET ... TO TRUE writes the value the name stands for.
IDENTIFICATION DIVISION.
PROGRAM-ID. C88.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FLAG-FIELD PIC X VALUE "N".
88 FLAG-IS-SET VALUE "Y".
88 FLAG-IS-CLEAR VALUE "N".
01 CODE-FIELD PIC 9(2) VALUE 42.
88 CODE-IN-RANGE VALUE 10 THRU 99.
PROCEDURE DIVISION.
SET FLAG-IS-SET TO TRUE.
IF FLAG-IS-SET
DISPLAY "flag is set"
END-IF.
IF CODE-IN-RANGE
DISPLAY "code 42 is in range"
END-IF.
SET FLAG-IS-CLEAR TO TRUE.
DISPLAY "flag now [" FLAG-FIELD "]".
STOP RUN.
cobc -x -free -Wall -o c88 c88.cob
./c88
flag is set
code 42 is in range
flag now [N]
VALUE 10 THRU 99 is what makes a condition name worth writing: one name for a range, tested in one line, and the range written once. The lesson on level-88 condition names goes further into the FALSE branch and into several values under one name.
What the numbers may not do
Four rules cover the mistakes:
- A group item has no picture. A 01 with children and a PIC X(4) stops the build with error: group item 'CUSTOMER' cannot have PICTURE clause.
- A 77 has no children, as the error above shows.
- A level number above 49 is not a level number: 50 is read as a word, and the data division ends where it stands.
- An 88 has a value and nothing else. A picture on it is refused with error: syntax error, unexpected PICTURE, expecting VALUE or VALUES.
Two more levels exist and both come later: level 66 for a RENAMES of a group and level 78 for a compile-time constant. The lessons on RENAMES and constants take them.
Exercises
- Add a second address to the customer record as a 05 group with a street and a city, and print the length of the whole record before and after. Which number in the record changed?
- Give the 01 of the record a PIC X(4) and build. Copy the error line, then remove the picture.
- Declare an 88 called CODE-IS-ZERO under the code field with VALUE 0, set it true, and print the field. What is in it, and what does the length of the record say?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does an entry with a name, no picture and children describe?
Answer
A group item
A group has children and no bytes of its own; an elementary item has a picture and no children.
-
Which level number marks a field that belongs to no record?
Answer
77
A 77 item is independent: it has no parent, no children and no place in any record.
-
What is a level 88?
Answer
A condition name for a value of the field above it
An 88 takes no storage: it names one value, or a range with THRU, and an IF or a SET may use the name.
-
How long is a group item?
Answer
The sum of the fields under it, in declaration order
A group is an overlay on its children: its bytes are their bytes, so LENGTH OF a group is their sum.
-
What is printed by
DISPLAY LENGTH OF Rfor this record?01 R. 05 A PIC X(3). 05 B PIC 9(2).Answer
5
The record is a group, so its length is the three characters of A and the two digits of B.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 FLAG-FIELD PIC X VALUE "N". 88 FLAG-IS-SET VALUE "Y". PROCEDURE DIVISION. SET FLAG-IS-SET TO TRUE. DISPLAY "[" FLAG-FIELD "]". STOP RUN.Answer
[Y]
SET ... TO TRUE writes the value the condition name stands for, and the field itself is what the DISPLAY prints.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 CUSTOMER PIC X(4). 05 CUSTOMER-ID PIC 9(4). PROCEDURE DIVISION. DISPLAY CUSTOMER-ID. STOP RUN.Answer
error: group item 'CUSTOMER' cannot have PICTURE clause
A 01 with children is a group, and a group has no picture: the bytes come from its children, not from a mask of its own.
-
What is the first line of the compiler output for this program?
IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 77 COUNTER-FIELD PIC 9(3). 05 PART PIC 9. PROCEDURE DIVISION. DISPLAY COUNTER-FIELD. STOP RUN.Answer
error: level number must begin with 01 or 77
A 77 cannot be followed by a lower level, so the entry after it has to begin a new 01 or a new 77.
-
An 88 is written with a PICTURE clause. What does the build do?
Answer
It stops with
error: syntax error, unexpected PICTURE, expecting VALUE or VALUESA condition name has a value and nothing else: it takes no storage, so a picture has no meaning on it.
-
A record declares 01, then 05, then 10. What does the compiler do?
Answer
It builds: the numbers only have to rise, not to be consecutive
The numbers are a hierarchy, not a count: 01, 05 and 10 describe three nested levels and nothing depends on the gaps.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev