Analysis of SAMPLE1.CBL - Bristol Community College

Download Report

Transcript Analysis of SAMPLE1.CBL - Bristol Community College

Analysis of SAMPLE1.CBL
Please check speaker notes for
additional information!
IDENTIFICATION DIVISION.
IDENTIFICATION DIVISION,
PROGRAM-ID, and AUTHOR are
reserved words. They must be
coded in exactly this way. Note
also the use of periods. The
periods are required.
IDENTIFICATION DIVISION.
PROGRAM-ID.
SAMPLE1.
AUTHOR. GROCER.
The name of the
programmer is optional
to COBOL but required
for this course.
These commands are written
starting in column 8 which is also
referred to as margin A.
The name of the program is provided by the
programmer. It should be 8 characters or less,
made up of letters and numbers. The first
character should be a letter. This is a required
entry.
ENVIRONMENT DIVISION.
CUSTOMER-FILE and
CUSTOMER-REPORT are
the names of the logical file
that the programmer made up.
Names must consist of letters,
numbers and hyphens only.
Column 8
Margin A
Column 12
Margin B
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CUSTOMER-FILE
ASSIGN TO "C:\PCOBWIN\CIS12FST\C12FIRST.DAT".
SELECT CUSTOMER-REPORT
ASSIGN TO PRINTER.
The RESERVED words
must be coded as
required by COBOL.
The ASSIGN TO clause points
to the physical location of the
file. The CUSTOMER-FILE is
being read from a disk and the
CUSTOMER-REPORT is being
written to the PRINTER.
Column 16
Still Margin B
Note the different
assignments. Above I
am saying the customer
file will be found on
the A drive as
CUSTP00.DAT. To the
right, I am saying it
will be found on the C
drive under
PCOBWIN, CIS12FSR
and will be named
C12FIRST.DAT. When
dealing with the select
the programmer must
know the name and
location.
Program
DATA DIVISION.
The DATA DIVISION is made
up of two sections, the FILE
SECTION and the
WORKING-STORAGE
SECTION.
Column 8
Margin A
Column 12
Margin B
DATA DIVISION.
FILE SECTION.
FD stands for file description. The name after the FD
was the name used in the SELECT statement in the
ENVIRONMENT DIVISION - CUSTOMER-FILE.
DATA DIVISION.
FILE SECTION.
FD CUSTOMER-FILE
DATA RECORD IS CUSTOMER-RECORD.
01 CUSTOMER-RECORD.
05 CUSTOMER-ID
05 CUSTOMER-NAME
05 CUSTOMER-STREET
05 CUSTOMER-CITY
05 CUSTOMER-STATE
05 CUSTOMER-ZIP
05 FILLER
Each record on the record is described on
the 05 level in this program. Each field is
given a unique name and the type and
length are specified.
PIC
PIC
PIC
PIC
PIC
PIC
PIC
CUSTOMER-FILE is the name of the
whole file - all of the records in the file.
Each record when it is referred to
individually is called CUSTOMERRECORD. The record name
CUSTOMER-RECORD is then repeated
at the 01 level of the FD.
X(4).
X(20).
X(20).
X(15).
X(2).
X(5).
X(10).
Reserved words must be
used as specified in
COBOL.
X means that the data is
alphanumeric (letters, number,
special characters). The number
inside the parenthesis is describing
the length of the field.
Record Layout Input File
1234Jane Doe
2345Ann Smith
3456Susan Ash
123 Elm St
45 Oak St
234 Maple St
DATA DIVISION.
FILE SECTION.
FD CUSTOMER-FILE
DATA RECORD IS CUSTOMER-RECORD.
01 CUSTOMER-RECORD.
05 CUSTOMER-ID
05 CUSTOMER-NAME
05 CUSTOMER-STREET
05 CUSTOMER-CITY
05 CUSTOMER-STATE
05 CUSTOMER-ZIP
05 FILLER
PIC
PIC
PIC
PIC
PIC
PIC
PIC
Fall River
Braintree
Weymouth
X(4).
X(20).
X(20).
X(15).
X(2).
X(5).
X(10).
MA02771
MA02184
MA02180
DATA DIVISION.
FILE SECTION.
(continued)
CUSTOMER-REPORT is the output report that has been assigned
to the printer. Each line on that report is called PRINTZ.
FD
01
Each field is
given a unique
name. I like to
use the name I
used on the input
record and add
the -PR to make it
unique. Then I
know what data is
going to the field.
CUSTOMER-REPORT
DATA RECORD IS PRINTZ.
PRINTZ.
05 FILLER
05 CUSTOMER-ID-PR
05 FILLER
05 CUSTOMER-NAME-PR
05 FILLER
05 CUSTOMER-STREET-PR
05 FILLER
05 CUSTOMER-CITY-PR
05 FILLER
05 CUSTOMER-STATE-PR
05 FILLER
05 CUSTOMER-ZIP-PR
05 FILLER
FILLER is used to put spaces between the fields.
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
X.
X(4).
X(2).
X(20).
X(2).
X(20).
X(2).
X(15).
X(2).
X(2).
X(2).
X(5).
X(3).
80 characters
Record Layout Output File
FD
01
1234
2345
3456
Jane Doe
Ann Smith
Susan Ash
CUSTOMER-REPORT
DATA RECORD IS PRINTZ.
PRINTZ.
05 FILLER
05 CUSTOMER-ID-PR
05 FILLER
05 CUSTOMER-NAME-PR
05 FILLER
05 CUSTOMER-STREET-PR
05 FILLER
05 CUSTOMER-CITY-PR
05 FILLER
05 CUSTOMER-STATE-PR
05 FILLER
05 CUSTOMER-ZIP-PR
05 FILLER
123 Elm St
45 Oak St
234 Maple St
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
Fall River
Braintree
Weymouth
X.
X(4).
X(2).
X(20).
X(2).
X(20).
X(2).
X(15).
X(2).
X(2).
X(2).
X(5).
X(3).
MA
MA
MA
02771
02184
02180
DATA DIVISION.
WORKING-STORAGE SECTION.
There is one entry here - the
END-OF-FILE field set up in
memory. In COBOL we try to
group the things in the
WORKING-STORAGE
SECTION, so I have the name
INDICATORS here. If there
were other indicators, they
would all be grouped as
separate 05 fields under the 01.
WORKING-STORAGE SECTION.
01 INDICATORS.
05 END-OF-FILE
PIC XXX
VALUE "NO ".
This sets up a three character field called END-OF-FILE in memory and gives it an initial value
of NO followed by a space (since there is room for 3 characters).
PROCEDURE DIVISION.
The PROCEDURE DIVISION
is made up of paragraphs.
Each paragraph contains
instructions to the computer.
Paragraph names start in
column 8 (margin A)while
instruction start in column 12
(margin B).
Column 8
Margin A
Paragraphs
Column 12
Margin B
Instructions
MAINLINE
PROCESS FILE
PERFORM
INITIALIZE
READ
INITIAL
RECORD
PROCESS RECORD
SET UP LINE
- clear
- move data
WRITE LINE
PERFORM
PROCESS
FILE
EOF
Y Move YES to
EOF indicator
READ
RECORD
N
PERFORM
WRAPUP
STOP RUN
EOF
indicator
NOT =
YES
Y
N
END PROCESS FILE
PERFORM
PROCESS
RECORD
EOF
N
ENDPROCESS RECORD
INITIALIZE
WRAPUP
OPEN FILES
CLOSE FILES
END INITIALIZE
Y Move YES to
EOF indicator
END WRAPUP
MAINLINE
PROCESS FILE
PERFORM
INITIALIZE
READ
INITIAL
RECORD
PROCESS RECORD
SET UP LINE
- clear
- move data
WRITE LINE
PERFORM
PROCESS
FILE
EOF
Y
Move YES to
EOF indicator
READ
RECORD
N
PERFORM
WRAPUP
STOP RUN
EOF
indicator
NOT =
YES
Y
PERFORM
PROCESS
RECORD
EOF
Y
N
N
END PROCESS FILE
ENDPROCESS RECORD
INITIALIZE
WRAPUP
OPEN FILES
CLOSE FILES
END INITIALIZE
END WRAPUP
Move YES to
EOF indicator
PROCEDURE
DIVISION.
MAIN-PROGRAM.
PERFORM A-100-INITIALIZATION.
PERFORM B-100-PROCESS-FILE.
PERFORM C-100-WRAP-UP.
STOP RUN.
A-100-INITIALIZATION.
OPEN INPUT CUSTOMER-FILE
OUTPUT CUSTOMER-REPORT.
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".
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.
CLOSE CUSTOMER-FILE
CUSTOMER-REPORT.
MAIN-PROGRAM controls the flow of the program. All
the things that it controls are at the 100 level. Things
involved with initialization have an A at the beginning of
the paragraph name, things involved with processing have
a B at the beginning of the paragraph name, things
involved with termination or wrap-up, have a C at the
beginning of the paragraph name.
MAIN-PROGRAM
A-100INITIALIZATION
B-100PROCESSFILE
C-100WRAP-UP
B-200PROCESSRECORD
The B-100 controls the processing. The loop that
actually processes each record is called the B-200
and it is controlled by the B-100.
MAIN-PROGRAM.
PERFORM A-100-INITIALIZATION.
PERFORM B-100-PROCESS-FILE.
PERFORM C-100-WRAP-UP.
STOP RUN.
A-100-INITIALIZATION.
OPEN INPUT CUSTOMER-FILE
OUTPUT CUSTOMER-REPORT.
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".
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.
CLOSE CUSTOMER-FILE
CUSTOMER-REPORT.
1234Jane Doe
7
1234
Jane Doe
8
123 Elm St
Fall River
9
10
123 Elm St
MA02771
11
Fall River
12
MA
02771
13
MAIN-PROGRAM.
PERFORM A-100-INITIALIZATION.
1
PERFORM B-100-PROCESS-FILE.
3
PERFORM C-100-WRAP-UP.
1234 Jane Doe
STOP RUN.
A-100-INITIALIZATION.
OPEN INPUT CUSTOMER-FILE
2
OUTPUT CUSTOMER-REPORT.
B-100-PROCESS-FILE.
4
READ CUSTOMER-FILE
1234Jane Doe
123 Elm
AT END
MOVE "YES" TO END-OF-FILE.
5
PERFORM B-200-PROCESS-RECORD
15
UNTIL END-OF-FILE = "YES".
B-200-PROCESS-RECORD.
6
MOVE SPACES TO PRINTZ.
MOVE CUSTOMER-ID TO CUSTOMER-ID-PR.
7
8
MOVE CUSTOMER-NAME TO CUSTOMER-NAME-PR.
9
MOVE CUSTOMER-STREET TO CUSTOMER-STREET-PR.
10
MOVE CUSTOMER-CITY TO CUSTOMER-CITY-PR.
11
MOVE CUSTOMER-STATE TO CUSTOMER-STATE-PR.
MOVE CUSTOMER-ZIP TO CUSTOMER-ZIP-PR.
12
WRITE PRINTZ
13
AFTER ADVANCING 1 LINE.
2345Ann Smith
14
READ CUSTOMER-FILE
AT END
MOVE "YES" TO END-OF-FILE.
C-100-WRAP-UP.
CLOSE CUSTOMER-FILE
CUSTOMER-REPORT.
123 Elm St
St
Fall River
45 Oak St
Fall River
MA
MA02771
Braintree
MA02184
02771
2345Ann Smith
17
2345
18
Ann Smith
45 Oak St
Braintree
19
MA02184
20
45 Oak St
21
Braintree
22
MA
02184
23
MAIN-PROGRAM.
PERFORM A-100-INITIALIZATION.
PERFORM B-100-PROCESS-FILE.
PERFORM C-100-WRAP-UP.
1234 Jane Doe
STOP RUN.
2345 Ann Smith
A-100-INITIALIZATION.
OPEN INPUT CUSTOMER-FILE
OUTPUT CUSTOMER-REPORT.
B-100-PROCESS-FILE.
READ CUSTOMER-FILE
AT END
MOVE "YES" TO END-OF-FILE.
PERFORM B-200-PROCESS-RECORD
15
UNTIL END-OF-FILE = "YES".
25
B-200-PROCESS-RECORD.
16
MOVE SPACES TO PRINTZ.
MOVE CUSTOMER-ID TO CUSTOMER-ID-PR.
17
18
MOVE CUSTOMER-NAME TO CUSTOMER-NAME-PR.
19
MOVE CUSTOMER-STREET TO CUSTOMER-STREET-PR.
20
MOVE CUSTOMER-CITY TO CUSTOMER-CITY-PR.
21
MOVE CUSTOMER-STATE TO CUSTOMER-STATE-PR.
MOVE CUSTOMER-ZIP TO CUSTOMER-ZIP-PR.
22
WRITE PRINTZ
23
AFTER ADVANCING 1 LINE.
3456Susan Ash
24
READ CUSTOMER-FILE
AT END
MOVE "YES" TO END-OF-FILE.
C-100-WRAP-UP.
CLOSE CUSTOMER-FILE
CUSTOMER-REPORT.
123 Elm St
45 Oak St
234 Maple St
Fall River
Braintree
Weymouth
MA02180
MA 02771
MA 02184
3456Susan Ash
27
3456
Susan Ash
28
234 Maple St
Weymouth
29
30
234 Maple St
Weymouth
MA02180
31
32
MA
02180
33
MAIN-PROGRAM.
PERFORM A-100-INITIALIZATION.
PERFORM B-100-PROCESS-FILE.
PERFORM C-100-WRAP-UP.
37
1234 Jane Doe
39
STOP RUN.
2345 Ann Smith
A-100-INITIALIZATION.
3456 Susan Ash
OPEN INPUT CUSTOMER-FILE
OUTPUT CUSTOMER-REPORT.
B-100-PROCESS-FILE.
READ CUSTOMER-FILE
AT END
MOVE "YES" TO END-OF-FILE.
PERFORM B-200-PROCESS-RECORD
25
UNTIL END-OF-FILE = "YES".
36
B-200-PROCESS-RECORD.
26
MOVE SPACES TO PRINTZ.
MOVE CUSTOMER-ID TO CUSTOMER-ID-PR.
27
28
MOVE CUSTOMER-NAME TO CUSTOMER-NAME-PR.
29
MOVE CUSTOMER-STREET TO CUSTOMER-STREET-PR.
30
MOVE CUSTOMER-CITY TO CUSTOMER-CITY-PR.
31
MOVE CUSTOMER-STATE TO CUSTOMER-STATE-PR.
MOVE CUSTOMER-ZIP TO CUSTOMER-ZIP-PR.
32
WRITE PRINTZ
33
AFTER ADVANCING 1 LINE.
34
READ CUSTOMER-FILE
AT END
35
MOVE "YES" TO END-OF-FILE.
C-100-WRAP-UP.
CLOSE CUSTOMER-FILE
38
CUSTOMER-REPORT.
123 Elm St
45 Oak St
234 Maple St
Fall River
Braintree
Weymouth
END-OF-FILE
YES
MA 02771
MA 02184
MA 02180