Looping in your COBOL Program - PowerPoint PPT Presentation

About This Presentation
Title:

Looping in your COBOL Program

Description:

05 GET-ACCEPT-ANS PIC X VALUE SPACES. 05 FIRST-NUMBER PIC 999 VALUE 0. ... 05 STOP-PROCESSING PIC X. Create1.cbl. This program is going to create a file. ... – PowerPoint PPT presentation

Number of Views:108
Avg rating:3.0/5.0
Slides: 24
Provided by: priscill1
Category:
Tags: cobol | looping | pic | program

less

Transcript and Presenter's Notes

Title: Looping in your COBOL Program


1
Looping in your COBOL Program
  • Please see speaker notes for additional
    information!

2
ADDDALP.CBL
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDLOOP. AUTHOR. GROCER.
ENVIRONMENT DIVISION. DATA DIVISION.
WORKING-STORAGE SECTION. 01
INPUT-AREA. 05 GET-ACCEPT-ANS PIC
X VALUE SPACES. 05 FIRST-NUMBER
PIC 999 VALUE 0. 05
SECOND-NUMBER PIC 999 VALUE 0.
05 ADD-ANS PIC 9999 VALUE 0.
PROCEDURE DIVISION. MAINLINE.
PERFORM B-100-PROCESS UNTIL
GET-ACCEPT-ANS "Q". STOP RUN.
B-100-PROCESS. DISPLAY "ENTER THE
FIRST NUMBER (UNDER 1000)". ACCEPT
FIRST-NUMBER. DISPLAY "ENTER THE
SECOND NUMBER (UNDER 1000)". ACCEPT
SECOND-NUMBER. ADD FIRST-NUMBER TO
SECOND-NUMBER GIVING ADD-ANS.
DISPLAY "THE ANSWER IS " ADD-ANS.
DISPLAY "PRESS Q FOLLOWED BY ENTER TO END THE
PROGRAM". DISPLAY "PRESS ENTER ALONE
TO CONTINUE". ACCEPT GET-ACCEPT-ANS.
This is a traditional way of labeling paragraphs.
We will see more about this in future slides.
The PERFORMUNTIL loop means do the paragraph
over and over again until the user enters Q.
3
Logic of PERFORMUNTIL
PERFORM B-100-PROCESS UNTIL GET-ACCEPT-ANS
"Q". STOP RUN.
GET-ACCEPT-ANS NOT Q
PERFORM B-100-PROCESS
YES
NO
The logic of the PERFORMUNTIL is that the UNTIL
is checked first. If the condition is not true,
then the routine is performed. If the condition
is true then the logic drops to the next command
which in this case is STOP RUN.
STOP RUN
4
ADDDALP.INT
5
ADDDALP.INT
6
Create1.cbl
IDENTIFICATION DIVISION.
PROGRAM-ID. CREATE1. AUTHOR. GROCER
ENVIRONMENT DIVISION. INPUT-OUTPUT
SECTION. FILE-CONTROL. SELECT
PAY-FILE ASSIGN TO "A\PAY.DAT".
DATA DIVISION. FILE SECTION.
FD PAY-FILE DATA RECORD IS PAY-REC.
01 PAY-REC. 05 PAY-ID
PIC 9999. 05 PAY-NAME
PIC X(20). 05 PAY-TYPE
PIC X. 05 PAY-RATE PIC
999V99. 05 PAY-HOURS PIC
99. WORKING-STORAGE SECTION. 01
WORK-AREA. 05 STOP-PROCESSING
PIC X.
This program is going to create a file. To do
this we need to make entries in the ENVIRONMENT
DIVISION. First we establish an INPUT-OUTPUT
SECTION (there can be different sections in the
ENVIRONMENT DIVISION), and then we specify that
we are going to do FILE-CONTROL. We now use the
SELECT statement to establish a link between the
physical file which is called PAY.DAT and will be
on the A drive and the logical file which we are
going to name PAY-FILE. Note that the ASSIGN
statement points to the physical file.
7
Create1.cbl
We are now adding the FILE SECTION to the DATA
DIVISION. The FILE SECTION will hold a
description of the file that we want to
write. First we put in FD which stands for File
Description. We then skip two spaces to get into
margin B and write the logical name of the file
that we used in the SELECT statement. This makes
a connection between the file that is being
written to the A drive as PAY.DAT and the
description that we are now giving. In the FD
clause I added the optional line DATA RECORD IS
PAY-REC. This establishes the name of the record
that we see repeated at the 01 level. Next, we
will describe the information on the record.
IDENTIFICATION DIVISION.
PROGRAM-ID. CREATE1. AUTHOR. GROCER
ENVIRONMENT DIVISION. INPUT-OUTPUT
SECTION. FILE-CONTROL. SELECT
PAY-FILE ASSIGN TO "A\PAY.DAT".
DATA DIVISION. FILE SECTION.
FD PAY-FILE DATA RECORD IS PAY-REC.
01 PAY-REC. 05 PAY-ID
PIC 9999. 05 PAY-NAME
PIC X(20). 05 PAY-TYPE
PIC X. 05 PAY-RATE PIC
999V99. 05 PAY-HOURS PIC
99. WORKING-STORAGE SECTION. 01
WORK-AREA. 05 STOP-PROCESSING
PIC X.
8
Create1.cbl
IDENTIFICATION DIVISION.
PROGRAM-ID. CREATE1. AUTHOR. GROCER
ENVIRONMENT DIVISION. INPUT-OUTPUT
SECTION. FILE-CONTROL. SELECT
PAY-FILE ASSIGN TO "A\PAY.DAT".
DATA DIVISION. FILE SECTION.
FD PAY-FILE DATA RECORD IS PAY-REC.
01 PAY-REC. 05 PAY-ID
PIC 9999. 05 PAY-NAME
PIC X(20). 05 PAY-TYPE
PIC X. 05 PAY-RATE PIC
999V99. 05 PAY-HOURS PIC
99. WORKING-STORAGE SECTION. 01
WORK-AREA. 05 STOP-PROCESSING
PIC X.
The fields on the PAY-REC are described under 01
PAY-REC. I put them all at 05 levels. This says
that the record I am going to write will have a 4
character numeric field first that I will refer
to in this program as PAY-ID. Next there will be
a 20 character alphanumeric field that I will
call PAY-NAME. Next there will be a 1 character
alphanumeric field that I will call PAY-TYPE.
After that comes a 5 character numeric field with
an assumed decimal point between the third and
the fourth characters. I will refer to this
field as PAY-RATE. Finally there will be a 2
character numeric field that I will refer to as
PAY-HOURS. When I add up the fields, I see that
the record is 32 characters long.
9
Pay.dat
This is the data file that is written - note that
it is one long string of data. I am going to
break it up into separate records for discussion.
1111John Doe F02500402222Susan Smith
F02500403333Linda Fox
P01575204444David Johnson P0145025
Note that there is an assumed decimal point (V)
between the third and fourth characters of
PAY-RATE so 02500 is treated like 025.00 and
01575 is treated like 015.75 even though the
actual decimal point is not there!
1111John Doe F0250040 2222Susan Smith
F0250040 3333Linda Fox
P0157520 4444David Johnson P0145025
01 PAY-REC. 05 PAY-ID
PIC 9999. 05 PAY-NAME
PIC X(20). 05 PAY-TYPE
PIC X. 05 PAY-RATE PIC
999V99. 05 PAY-HOURS PIC
99.
10
PROCEDURE DIVISION. MAINLINE.
PERFORM A-100-STARTUP. PERFORM
B-100-PROCESS. PERFORM C-100-WRAPUP.
STOP RUN. A-100-STARTUP.
OPEN OUTPUT PAY-FILE. B-100-PROCESS.
PERFORM B-200-LOOP UNTIL
STOP-PROCESSING "Y". B-200-LOOP.
DISPLAY "ENTER IDENTIFICATION NUMBER".
ACCEPT PAY-ID. DISPLAY "ENTER
NAME". ACCEPT PAY-NAME.
DISPLAY "ENTER JOB TYPE". ACCEPT
PAY-TYPE. DISPLAY "ENTER PAY RATE"
ACCEPT PAY-RATE. DISPLAY "ENTER
HOURS WORKED". ACCEPT PAY-HOURS.
WRITE PAY-REC. DISPLAY "PRESS
ENTER TO CONTINUE". DISPLAY "PRESS Y
FOLLOWED BY ENTER TO QUIT". ACCEPT
STOP-PROCESSING. C-100-WRAPUP.
CLOSE PAY-FILE.
CREATE1.CBL
11
PROCEDURE DIVISION. MAINLINE.
PERFORM A-100-STARTUP. - first command
executed PERFORM B-100-PROCESS. -
third command executed PERFORM
C-100-WRAPUP. STOP RUN.
A-100-STARTUP. OPEN OUTPUT PAY-FILE. -
second command executed B-100-PROCESS.
PERFORM B-200-LOOP - fourth command
executed UNTIL STOP-PROCESSING
"Y". B-200-LOOP. DISPLAY "ENTER
IDENTIFICATION NUMBER". ACCEPT
PAY-ID. DISPLAY "ENTER NAME".
ACCEPT PAY-NAME. DISPLAY "ENTER JOB
TYPE". ACCEPT PAY-TYPE.
DISPLAY "ENTER PAY RATE" ACCEPT
PAY-RATE. DISPLAY "ENTER HOURS
WORKED". ACCEPT PAY-HOURS.
WRITE PAY-REC. DISPLAY "PRESS ENTER TO
CONTINUE". DISPLAY "PRESS Y FOLLOWED
BY ENTER TO QUIT". ACCEPT
STOP-PROCESSING. C-100-WRAPUP.
CLOSE PAY-FILE.
CREATE1.CBL
Executing the fourth command means the UNTIL is
checked first. If STOP-PROCESSING is not equal
to Y then B-200-LOOP will be performed.
12
PROCEDURE DIVISION. MAINLINE.
PERFORM A-100-STARTUP. - first command
executed PERFORM B-100-PROCESS. -
third command executed PERFORM
C-100-WRAPUP. STOP RUN.
A-100-STARTUP. OPEN OUTPUT PAY-FILE. -
second command executed B-100-PROCESS.
PERFORM B-200-LOOP - fourth command
executed UNTIL STOP-PROCESSING
"Y". B-200-LOOP. DISPLAY "ENTER
IDENTIFICATION NUMBER". ACCEPT
PAY-ID. DISPLAY "ENTER NAME".
ACCEPT PAY-NAME. DISPLAY "ENTER JOB
TYPE". ACCEPT PAY-TYPE.
DISPLAY "ENTER PAY RATE" ACCEPT
PAY-RATE. DISPLAY "ENTER HOURS
WORKED". ACCEPT PAY-HOURS.
WRITE PAY-REC. DISPLAY "PRESS ENTER TO
CONTINUE". DISPLAY "PRESS Y FOLLOWED
BY ENTER TO QUIT". ACCEPT
STOP-PROCESSING. C-100-WRAPUP.
CLOSE PAY-FILE.
CREATE1.CBL
5thcommand executed 6th command executed 7th
command executed 8th command executed 9th command
executed 10th command executed 11th command
executed 12th command executed 13th command
executed 14th command executed 15th command
executed 16th command executed 17th command
executed 18th command executed
Assume Y not entered.
13
PROCEDURE DIVISION. MAINLINE.
PERFORM A-100-STARTUP. PERFORM
B-100-PROCESS. PERFORM C-100-WRAPUP.
STOP RUN. A-100-STARTUP.
OPEN OUTPUT PAY-FILE.
B-100-PROCESS. PERFORM B-200-LOOP -
19th command executed UNTIL
STOP-PROCESSING "Y". B-200-LOOP.
DISPLAY "ENTER IDENTIFICATION NUMBER".
ACCEPT PAY-ID. DISPLAY "ENTER
NAME". ACCEPT PAY-NAME.
DISPLAY "ENTER JOB TYPE". ACCEPT
PAY-TYPE. DISPLAY "ENTER PAY RATE"
ACCEPT PAY-RATE. DISPLAY "ENTER
HOURS WORKED". ACCEPT PAY-HOURS.
WRITE PAY-REC. DISPLAY "PRESS
ENTER TO CONTINUE". DISPLAY "PRESS Y
FOLLOWED BY ENTER TO QUIT". ACCEPT
STOP-PROCESSING. C-100-WRAPUP.
CLOSE PAY-FILE.
CREATE1.CBL
The until is checked in the 19th command to see
if a Y was entered. If not then B-200-LOOP is
performed again.
20thcommand executed 21st command executed 22nd
command executed 23rd command executed 24th
command executed 25th command executed 26th
command executed 27th command executed 28th
command executed 29th command executed 30th
command executed 31st command executed 32nd
command executed 33rd command executed
Assume Y not entered.
14
PROCEDURE DIVISION. MAINLINE.
PERFORM A-100-STARTUP. PERFORM
B-100-PROCESS. PERFORM C-100-WRAPUP.
STOP RUN. A-100-STARTUP.
OPEN OUTPUT PAY-FILE.
B-100-PROCESS. PERFORM B-200-LOOP -
34th command executed UNTIL
STOP-PROCESSING "Y". B-200-LOOP.
DISPLAY "ENTER IDENTIFICATION NUMBER".
ACCEPT PAY-ID. DISPLAY "ENTER
NAME". ACCEPT PAY-NAME.
DISPLAY "ENTER JOB TYPE". ACCEPT
PAY-TYPE. DISPLAY "ENTER PAY RATE"
ACCEPT PAY-RATE. DISPLAY "ENTER
HOURS WORKED". ACCEPT PAY-HOURS.
WRITE PAY-REC. DISPLAY "PRESS
ENTER TO CONTINUE". DISPLAY "PRESS Y
FOLLOWED BY ENTER TO QUIT". ACCEPT
STOP-PROCESSING. C-100-WRAPUP.
CLOSE PAY-FILE.
CREATE1.CBL
The until is checked in the 34th command to see
if a Y was entered. If not then B-200-LOOP is
performed again.
35thcommand executed 36th command executed 37th
command executed 38th command executed 39th
command executed 40th command executed 41st
command executed 42nd command executed 43rd
command executed 44th command executed 45th
command executed 46th command executed 47nd
command executed 48th command executed
Assume Y entered.
15
PROCEDURE DIVISION. MAINLINE.
PERFORM A-100-STARTUP. PERFORM
B-100-PROCESS. PERFORM C-100-WRAPUP.
STOP RUN. A-100-STARTUP.
OPEN OUTPUT PAY-FILE.
B-100-PROCESS. PERFORM B-200-LOOP -
49th command executed UNTIL
STOP-PROCESSING "Y". B-200-LOOP.
DISPLAY "ENTER IDENTIFICATION NUMBER".
ACCEPT PAY-ID. DISPLAY "ENTER
NAME". ACCEPT PAY-NAME.
DISPLAY "ENTER JOB TYPE". ACCEPT
PAY-TYPE. DISPLAY "ENTER PAY RATE"
ACCEPT PAY-RATE. DISPLAY "ENTER
HOURS WORKED". ACCEPT PAY-HOURS.
WRITE PAY-REC. DISPLAY "PRESS
ENTER TO CONTINUE". DISPLAY "PRESS Y
FOLLOWED BY ENTER TO QUIT". ACCEPT
STOP-PROCESSING. C-100-WRAPUP.
CLOSE PAY-FILE.
CREATE1.CBL
The until is checked in the 49th command to see
if a Y was entered. If not then B-200-LOOP is
performed again.
50th command executed 52nd command executed
The user entered a Y so B-200-LOOP will not be
PERFORMed again. The B-100-PROCESS paragraph is
now complete so control returns to the MAINLINE.
51st command executed
16
READ1.CBL
IDENTIFICATION DIVISION.
PROGRAM-ID. READ1. AUTHOR. GROCER
ENVIRONMENT DIVISION. INPUT-OUTPUT
SECTION. FILE-CONTROL. SELECT
PAY-FILE ASSIGN TO "A\PAY.DAT".
DATA DIVISION. FILE SECTION.
FD PAY-FILE DATA RECORD IS PAY-REC.
01 PAY-REC. 05 PAY-ID
PIC 9999. 05 PAY-NAME
PIC X(20). 05 PAY-TYPE
PIC X. 05 PAY-RATE PIC
999V99. 05 PAY-HOURS PIC
99. WORKING-STORAGE SECTION. 01
WORK-AREA. 05 EOF-IND
PIC X VALUE "N". 05
CONTINUE-ANS PIC X VALUE SPACES.
1111John Doe F0250040 2222Susan Smith
F0250040 3333Linda Fox
P0157520 4444David Johnson P0145025
17
PROCEDURE DIVISION.
MAINLINE. PERFORM A-100-STARTUP.
PERFORM B-100-PROCESS. PERFORM
C-100-WRAPUP. STOP RUN.
A-100-STARTUP. OPEN INPUT PAY-FILE.
B-100-PROCESS. READ PAY-FILE
AT END MOVE "Y" TO
EOF-IND. PERFORM B-200-LOOP
UNTIL EOF-IND "Y". B-200-LOOP.
DISPLAY "THE ID IS " PAY-ID.
DISPLAY "THE NAME IS " PAY-NAME.
DISPLAY "THE PAY TYPE IS " PAY-TYPE.
DISPLAY "THE PAY RATE IS " PAY-RATE.
DISPLAY "THE HOURS WORKED IS " PAY-HOURS.
DISPLAY " ". DISPLAY "PRESS ENTER
TO CONTINUE". ACCEPT CONTINUE-ANS.
READ PAY-FILE AT END
MOVE "Y" TO EOF-IND.
C-100-WRAPUP. CLOSE PAY-FILE.
READ1.CBL
Initializing READ reads the first record.
Read in the loop that reads all records after the
first record.
18
READ1.INT
1111John Doe F0250040 2222Susan Smith
F0250040 3333Linda Fox
P0157520 4444David Johnson P0145025
19
PROCEDURE DIVISION.
MAINLINE. PERFORM A-100-STARTUP. - 1st
command executed PERFORM
B-100-PROCESS. - 3rd command executed
PERFORM C-100-WRAPUP. STOP RUN.
A-100-STARTUP. OPEN INPUT PAY-FILE.
- 2nd command executed B-100-PROCESS.
READ PAY-FILE - 4th command
executed - record read AT END
1111John Doe F0250040
MOVE "Y" TO EOF-IND. PERFORM
B-200-LOOP - 5th command executed
UNTIL EOF-IND "Y". B-200-LOOP.
DISPLAY "THE ID IS " PAY-ID. - 6th
command executed DISPLAY "THE NAME IS
" PAY-NAME. - 7th command executed
DISPLAY "THE PAY TYPE IS " PAY-TYPE. - 8th
command exec DISPLAY "THE PAY RATE IS
" PAY-RATE. - 9th command exec DISPLAY
"THE HOURS WORKED IS " PAY-HOURS. - 10th command
DISPLAY " ". -
11th command executed DISPLAY "PRESS
ENTER TO CONTINUE".- 12th command executed
ACCEPT CONTINUE-ANS. - 13th
command executed READ PAY-FILE
- 14th command executed
AT END record
read MOVE "Y" TO EOF-IND.
C-100-WRAPUP. CLOSE PAY-FILE.
READ1
It is not EOF.
2222Susan Smith F0250040
20
PROCEDURE DIVISION.
MAINLINE. PERFORM A-100-STARTUP.
PERFORM B-100-PROCESS. PERFORM
C-100-WRAPUP. STOP RUN.
A-100-STARTUP. OPEN INPUT PAY-FILE.
B-100-PROCESS. READ PAY-FILE
AT END MOVE "Y" TO
EOF-IND. PERFORM B-200-LOOP - 15th
command executed UNTIL EOF-IND
"Y". B-200-LOOP. DISPLAY "THE
ID IS " PAY-ID. - 16th command executed
DISPLAY "THE NAME IS " PAY-NAME. - 17th
command executed DISPLAY "THE PAY TYPE
IS " PAY-TYPE. - 18th command exec
DISPLAY "THE PAY RATE IS " PAY-RATE. - 19th
command exec DISPLAY "THE HOURS WORKED
IS " PAY-HOURS. - 20th command DISPLAY
" ". - 21st command
executed DISPLAY "PRESS ENTER TO
CONTINUE".- 22nd command executed
ACCEPT CONTINUE-ANS. - 23rd command
executed READ PAY-FILE
- 24th command executed AT
END record read
MOVE "Y" TO EOF-IND. 3333Linda Fox
P0157520 C-100-WRAPUP.
CLOSE PAY-FILE.
READ1
The EOF-IND is not Y so B-200-LOOP will be
executed again.
It is not EOF.
21
PROCEDURE DIVISION.
MAINLINE. PERFORM A-100-STARTUP.
PERFORM B-100-PROCESS. PERFORM
C-100-WRAPUP. STOP RUN.
A-100-STARTUP. OPEN INPUT PAY-FILE.
B-100-PROCESS. READ PAY-FILE
AT END MOVE "Y" TO
EOF-IND. PERFORM B-200-LOOP - 25th
command executed UNTIL EOF-IND
"Y". B-200-LOOP. DISPLAY "THE
ID IS " PAY-ID. - 26th command executed
DISPLAY "THE NAME IS " PAY-NAME. - 27th
command executed DISPLAY "THE PAY TYPE
IS " PAY-TYPE. - 28th command exec
DISPLAY "THE PAY RATE IS " PAY-RATE. - 29th
command exec DISPLAY "THE HOURS WORKED
IS " PAY-HOURS. - 30th command DISPLAY
" ". - 31st command
executed DISPLAY "PRESS ENTER TO
CONTINUE".- 32nd command executed
ACCEPT CONTINUE-ANS. - 33rd command
executed READ PAY-FILE
- 34th command executed AT
END record read
MOVE "Y" TO EOF-IND. 4444David
Johnson P0145025 C-100-WRAPUP.
CLOSE PAY-FILE.
READ1
The EOF-IND is not Y so B-200-LOOP will be
executed again.
It is not EOF.
22
PROCEDURE DIVISION.
MAINLINE. PERFORM A-100-STARTUP.
PERFORM B-100-PROCESS. PERFORM
C-100-WRAPUP. STOP RUN.
A-100-STARTUP. OPEN INPUT PAY-FILE.
B-100-PROCESS. READ PAY-FILE
AT END MOVE "Y" TO
EOF-IND. PERFORM B-200-LOOP - 25th
command executed UNTIL EOF-IND
"Y". B-200-LOOP. DISPLAY "THE
ID IS " PAY-ID. - 26th command executed
DISPLAY "THE NAME IS " PAY-NAME. - 27th
command executed DISPLAY "THE PAY TYPE
IS " PAY-TYPE. - 28th command exec
DISPLAY "THE PAY RATE IS " PAY-RATE. - 29th
command exec DISPLAY "THE HOURS WORKED
IS " PAY-HOURS. - 30th command DISPLAY
" ". - 31st command
executed DISPLAY "PRESS ENTER TO
CONTINUE".- 32nd command executed
ACCEPT CONTINUE-ANS. - 33rd command
executed READ PAY-FILE
- 34th command executed AT
END no record is
read MOVE "Y" TO EOF-IND.
EOF-IND set to Y C-100-WRAPUP.
CLOSE PAY-FILE.
READ1
The EOF-IND Y so B-200-LOOP will not be executed
again.
It is EOF.
23
PROCEDURE DIVISION.
MAINLINE. PERFORM A-100-STARTUP.
PERFORM B-100-PROCESS. PERFORM
C-100-WRAPUP. - 36th command executed
STOP RUN. - 38th command executed
A-100-STARTUP. OPEN INPUT PAY-FILE.
B-100-PROCESS. READ PAY-FILE
AT END MOVE "Y" TO
EOF-IND. PERFORM B-200-LOOP - 35th
command executed UNTIL EOF-IND
"Y". B-200-LOOP. DISPLAY "THE
ID IS " PAY-ID. DISPLAY "THE
NAME IS " PAY-NAME. DISPLAY "THE PAY
TYPE IS " PAY-TYPE. DISPLAY "THE PAY
RATE IS " PAY-RATE. DISPLAY "THE
HOURS WORKED IS " PAY-HOURS. DISPLAY
" ". DISPLAY
"PRESS ENTER TO CONTINUE". ACCEPT
CONTINUE-ANS. READ
PAY-FILE AT
END
MOVE "Y" TO EOF-IND. C-100-WRAPUP.
CLOSE PAY-FILE. - 37th command
executed
READ1
Write a Comment
User Comments (0)
About PowerShow.com