Back to first page...


Please note that the typical COBOL program consists of 4 DIVISIONS.
       IDENTIFICATION DIVISION.
         .....
         .....
       ENVIRONMENT DIVISION.
         .....
         .....
       DATA DIVISION.
         .....
         .....
       PROCEDURE DIVISION.
All COBOL programs HAVE TO be constructed this way. Any COBOL program, with one of these divisions missing, simply won't work. No COBOL compiler will generate executables for incomplete source programs.


The lines
12345678901234567890    : Column indicator..
--------------------
       IDENTIFICATION DIVISION.
       PROGRAM-ID.  "sorttest".
       AUTHOR. CHARLIE BROWN.
      *
      *	Title:	sorttest.cbl
      *		RM/COBOL-85 Sort Test
      *
      *
identify th start of a COBOL source program. The lines that have an "*" on column 7 simply comments. One can key in anything as comments. In fact, those lines that have "anything" typed at column 7, are assumed to be comment lines and ignored by the RM-COBOL compiler. The only EXCEPTION is the "-" character which means the line is a CONTINUATION line; that is, the line is continuation of a previous line which didn't fit in the previous line. It is wise to always use "*" as the comment indicator because you cannot be sure which compiler you will have to use in the future.
12345678901234567890    : Column indicator..
--------------------
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       SOURCE-COMPUTER.  RMCOBOL-85.
       OBJECT-COMPUTER.  RMCOBOL-85.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT SORT-FILE ASSIGN TO DISK "\TEMP\sortwork.dat".
           SELECT MERGE-1 ASSIGN TO DISK "\DATA\merge1fl.dat".
           SELECT MERGE-2 ASSIGN TO DISK " \NEWDATA\merge2fl.dat".
ENVIRONMENT DIVISION is where the programmer describes what sort of hardware is targeted while the code has been developed and on which PHYSICAL devices the files involved are residing (or will reside while the program executes). In this example, it is seen that divisions can futher be divided into SECTIONS. For example, actual file names are related to COBOL file names in the FILE-CONTROL section. If the program runs in a UNIX environment, actual file names would look like /home/ugur/temp/sortwork rather than the above MS-DOS names. With the help of this section, programmer will relate actual file names ("\TEMP\sortwork.dat" etc) to COBOL file names ( SORT-FILE, MERGE-1 etc) and forget about file naming conventions. The actual file names will never be used in the program again and if you decide to port your code to a different computer running on a different operating system, all you have to change will be the few lines in the FILE-CONTROL section of the ENVIRONMENT DIVISION.



      *
       DATA DIVISION.
       FILE SECTION.
       SD  SORT-FILE.
       01  SORT-RECORD.
           02  RECORD-NUMBER            PIC 99.
           02                           PIC X(04).
           02  NAME                     PIC X(10).
           02                           PIC X(4).
           02  ZIP                      PIC 9(5).
           02                           PIC X(04).
           02  FILE-NUMBER              PIC 9.

       FD  MERGE-1
           BLOCK CONTAINS 8 RECORDS
           DATA RECORD IS MERGE1-OUT-RECORD.
       01  MERGE1-OUT-RECORD            PIC X(30).

       FD  MERGE-2
           BLOCK CONTAINS 8 RECORDS
           DATA RECORD IS MERGE2-OUT-RECORD.
       01  MERGE2-OUT-RECORD            PIC X(30).
      *
       WORKING-STORAGE SECTION.
       01  TITLE                 PIC X(60) VALUE
           "RM/COBOL-85 Verify SORT/MERGE functions - Demo Program".

       01  DATA-AREA.
           02  DATA-FILLER.
               03  PIC X(15) VALUE "SMITH     01023".
               03  PIC X(15) VALUE "JONES     90274".
               03  PIC X(15) VALUE "SMITH     90024".
               03  PIC X(15) VALUE "BROWN     02345".
               03  PIC X(15) VALUE "WONG      01456".
               03  PIC X(15) VALUE "JONES     45729".
               03  PIC X(15) VALUE "SMITH     78945".
               03  PIC X(15) VALUE "ADAMS     38217".
               03  PIC X(15) VALUE "SMITH     38217".
               03  PIC X(15) VALUE "MORRIS    86721".
               03  PIC X(15) VALUE "SMITH     01023".
               03  PIC X(15) VALUE "ROBERTS   46575".
               03  PIC X(15) VALUE "YOUNG     72365".
               03  PIC X(15) VALUE "SMITH     58724".
               03  PIC X(15) VALUE "STEVENSON 24345".
               03  PIC X(15) VALUE "DAVID     89431".
               03  PIC X(15) VALUE "SMITH     68752".
               03  PIC X(15) VALUE "JACKSON   31547".
           02  TEST-DATA  REDEFINES DATA-FILLER OCCURS 18 TIMES.
               03  TEST-NAME           PIC X(10).
               03  TEST-ZIP            PIC 9(5).

       01  COUNT-WK                    PIC 99 BINARY VALUE ZEROS.

       01  TEMP-X                      PIC X.

       01  CRT-LINE                    PIC 99 BINARY.
       77  SORT-AT-END                 PIC 9  VALUE 1.
       77  ALTERNATING-FLAG            PIC 9  VALUE 1.
DATA DIVISION is the part of the COBOL program where you declare all sorts of variables, arrays and structures that you use in your program. THE PROGRAMMER HAS TO DECLARE ALL VARIABLES, ARRAYS AND STRUCTURES. One cannot use a variable without first declaring it in the DATA DIVISON!

In COBOL, you have to declare items in different SECTIONS depending on the usage of these items. For example, structures related to files (description of file record formats) have to be declared in a FILE SECTION.

Temporary variables, temp arrays and temp structures have to be declared in a WORKING-STORAGE SECTION.

The numbers (like 01, 03, 88 etc) at the beginning of each declaration statement have special meanings. The values describe the hierarchy of variables with respect to each other. We shall study these hierarchies and the meanings of different values later in the course. To keep you informed, I would like to tell you that the contsruct

    01  SORT-RECORD.
           02  RECORD-NUMBER            PIC 99.
           02                           PIC X(04).
           02  NAME                     PIC X(10).
           02                           PIC X(4).
           02  ZIP                      PIC 9(5).
           02                           PIC X(04).
           02  FILE-NUMBER              PIC 9.
is describing a file record 30 bytes long (2+4+10+4+5+4+1), with columns 3-6, 17-20, 26-29 either blank or not-used and the remaining columns having useful information (record-number, name, zip code and file-number). These pieces of information are grouped (structured) under the collective name "SORT-RECORD" and each item can be referenced by their individual names; or can be referenced collectively with the structure name "SORT-RECORD".


Please note that all COBOL statements end with a point, or rather named "full stop". The usage of the point is very similar to the usage of ";" in C and Pascal languages.

      *
       PROCEDURE DIVISION.
       MAIN-PGM.
           DISPLAY TITLE, LOW, LINE 1, ERASE.
           .......
           .......
           .......

And finally the PROCEDURE division where all of the actual work is done... The executable statements, loops, comparisons etc are all coded into this section. Please note the English language look-alike statements. While you browse through the PROCEDURE DIVISION, you can easily follow what is going on and what the program is doing. These COBOL language constructs also make debugging extremely easy.


Next section : Elements of the COBOL Language...