SAMPLE1.CBL with Input/Output and comments


This program shows the input and output. They are included at the end of the program as comment lines in the program.

       IDENTIFICATION DIVISION.
      *An * in column 7 means that the line is a comment and will be
      *ignored when the program is being executed.
      *This division provides identification information such as
      *program-id (required) and author.
       PROGRAM-ID.   SAMPLE1.
       AUTHOR.  GROCER.
       ENVIRONMENT DIVISION.
      *This division provides information about the environment that
      *the program is being run in.  Specifically, we identify the
      *input file to be read and the output file to be written.  In
      *this program, the input file has the logical name of
      *CUSTOMER-FILE and physically it is located on the C drive, in
      *the directory/folder CIS12FST which is in the directory/folder
      *PCOBWIN.  The name of the file is C12FIRST.DAT.  The output
      *file has the logical name CUSTOMER-REPORT and the physical
      *output has been assigned to the printer.  Note that what we are
      *giving here is the path to find the file.  If the input had been
      *located on the root of the A drive, the path would be
      *A:\C12FIRST.DAT.  If we wanted to assign the output to a disk
      *file instead of the printer, the ASSIGN clause would contain a
      *path.  For example if we wanted to call it FIRSTOUT.DAT and
      *put ito on the printer, we would use
      *ASSIGN TO A:\FIRSTOUT.DAT instead of ASSIGN TO PRINTER.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT CUSTOMER-FILE
               ASSIGN TO "C:\PCOBWIN\CIS12FST\C12FIRST.DAT".
           SELECT CUSTOMER-REPORT
               ASSIGN TO "A:\FIRSTOUT.DAT".
      *        ASSIGN TO PRINTER.
       DATA DIVISION.
      *In this program, there are two sections of the DATA DIVISION
      *being used.  The FILE SECTION describes the layout of the files
      *being processed.  The first FD for CUSTOMER-FILE describes the
      *layout of the input record being read on the 01 level
      *and the FD for CUSTOMER-REPORT describes the layout of the
      *line being printed on the report on the 01 level.
       FILE SECTION.
       FD  CUSTOMER-FILE
           DATA RECORD IS CUSTOMER-RECORD.
       01  CUSTOMER-RECORD.
           05  CUSTOMER-ID                  PIC X(4).
           05  CUSTOMER-NAME                PIC X(20).
           05  CUSTOMER-STREET              PIC X(20).
           05  CUSTOMER-CITY                PIC X(15).
           05  CUSTOMER-STATE               PIC X(2).
           05  CUSTOMER-ZIP                 PIC X(5).
           05  FILLER                       PIC X(10).
       FD  CUSTOMER-REPORT
           DATA RECORD IS PRINTZ.
       01  PRINTZ.
           05  FILLER                       PIC X.
           05  CUSTOMER-ID-PR               PIC X(4).
           05  FILLER                       PIC X(2).
           05  CUSTOMER-NAME-PR             PIC X(20).
           05  FILLER                       PIC X(2).
           05  CUSTOMER-STREET-PR           PIC X(20).
           05  FILLER                       PIC X(2).
           05  CUSTOMER-CITY-PR             PIC X(15).
           05  FILLER                       PIC X(2).
           05  CUSTOMER-STATE-PR            PIC X(2).
           05  FILLER                       PIC X(2).
           05  CUSTOMER-ZIP-PR              PIC X(5).
           05  FILLER                       PIC X(3).
       WORKING-STORAGE SECTION.
      *The WORKING-STORAGE SECTION defines work areas that are being
      *used by the program. The work areas are grouped by use for
      *organization purposes.  In this program, there is one work area
      *that is being used to tell when end of file has been reached.
      *The work area has been named END-OF-FILE and has been assigned
      *three characters which have been set to an initial value of NO .
      *The grouping is under 01 INDICATORS (a name I made up) - if there
      *were additional indicators in this program, I would have put
      *them in as 05 level fields under this grouping.
       01  INDICATORS.
           05  END-OF-FILE                  PIC XXX      VALUE "NO ".
       PROCEDURE DIVISION.
      *The PROCEDURE DIVISION is where the actual instructions to
      *process the data are given.  The procedure division is
      *organized into paragraphs that contain instructions to accomplish
      *specified tasks.  In the style of programming being demonstrated
      *here, the paragraph MAIN-PROGRAM controls the program.
      *First the setup and preparation are done by having the program
      *go and perform the paragraph A-100-INITIALIZATION.  Then the
      *processing is done by having the program go and perform the
      *paragraph B-100-PROCESS-FILE.  Finally, when the end of file
      *has been reached and the processing is complete, the program
      *will go and perform C-100-WRAP-UP to close the files and do any
      *final processing.  The STOP RUN statement then terminates the
      *program.
       MAIN-PROGRAM.
           PERFORM A-100-INITIALIZATION.
           PERFORM B-100-PROCESS-FILE.
           PERFORM C-100-WRAP-UP.
           STOP RUN.
      *The initialization paragraph simply opens the disk file that is
      *to be read as input and the print file that is to be written as
      *output.
       A-100-INITIALIZATION.
           OPEN INPUT CUSTOMER-FILE
                OUTPUT CUSTOMER-REPORT.
      *The process paragraph does the initializing read of the first
      *record in the file and then performs the paragraph named
      *B-200-PROCESS-RECORD over and over again until the END-OF-FILE
      *indicator has been reset from NO to YES.
       B-100-PROCESS-FILE.
           READ CUSTOMER-FILE
               AT END
                  MOVE "YES" TO END-OF-FILE.
           PERFORM B-200-PROCESS-RECORD
               UNTIL END-OF-FILE = "YES".
      *The paragraph that is performed over and over again is the
      *B-200-PROCESS-RECORD paragraph which in this program moves data
      *from the input record that has just been read to the output line,
      *writes the line and then reads another record.  If the read
      *indicates that there are no more records on the file the
      *indicator gets changed from NO to YES which according to the
      *PERFORM statement above means that the loop will not be executed
      *again.
      *The flow here is important to understand.
      *The MAIN-PROGRAM paragraph said to perform B-100-PROCESS-FILE.
      *When the program got to B-100-PROCESS-FILE, it read a record
      *and then was told to perform B-200-PROCESS-RECORD over and over
      *again until the end of file indicator was changed from NO to YES.
      *The processing then went and did the B-200-PROCESS-RECORD one
      *time, checked the UNTIL clause on the PERFORM in the
      *B-100-PROCESS-FILE paragraph to see if it should do
      *B-200-PROCESS-RECORD again - the indicator wasn't YES so it did
      *it again.  This checking and then performing again happens over
      *and over again until the indicator contains a YES.
      *The AT END clause on the READ statement is what causes the
      *indicator to be changed to a YES - if end of file has been
      *reached and there are no more records to be read and processed
      *the AT END clause is executed and the indicator is changed to
      *YES.
       B-200-PROCESS-RECORD.
           MOVE SPACES TO PRINTZ.
           MOVE CUSTOMER-ID TO CUSTOMER-ID-PR.
           MOVE CUSTOMER-NAME TO CUSTOMER-NAME-PR.
           MOVE CUSTOMER-STREET TO CUSTOMER-STREET-PR.
           MOVE CUSTOMER-CITY TO CUSTOMER-CITY-PR.
           MOVE CUSTOMER-STATE TO CUSTOMER-STATE-PR.
           MOVE CUSTOMER-ZIP TO CUSTOMER-ZIP-PR.
           WRITE PRINTZ
               AFTER ADVANCING 1 LINE.
           READ CUSTOMER-FILE
               AT END
                  MOVE "YES" TO END-OF-FILE.
       C-100-WRAP-UP.
      *This is the paragraph that is performed after processing is
      *complete.  In this program, it simply closes the files.
           CLOSE CUSTOMER-FILE
                 CUSTOMER-REPORT.
      *
      *Note: An * in col 7 makes the line a comment.
      *The input file being processed by this program contains the
      *following three records:
      *
      *1234Jane Doe            123 Elm St          Fall River     MA02771
      *2345Ann Smith           45 Oak St           Braintree      MA02184
      *3456Susan Ash           234 Maple St        Weymouth       MA02180
      *
      *The output that is produced is:
      *
      * 1234  Jane Doe              123 Elm St            Fall River       MA  02771
      * 2345  Ann Smith             45 Oak St             Braintree        MA  02184
      * 3456  Susan Ash             234 Maple St          Weymouth         MA  02180
      
The lines above show the input records that were read by this program and the output lines that were produced. I added these lines as comments in the program using cut and paste techniques.