Chapter 4
Coding Complete COBOL
Programs: The PROCEDURE
DIVISION
4-1
Chapter Objectives
To familiarize you with methods used to
1. Access input and output files
2. Read data from an input file
3. Perform simple move operations
4. Write information to an output file
5. Accomplish end-of-job operations
6. Execute paragraphs from main module,
return control to that main module
4-2
Review of First Three Divisions
• IDENTIFICATION DIVISION
– Identifies program name
• ENVIRONMENT DIVISION
– Defines files and equipment used by batch
programs
– Associates a logical / programmer-defined
file name with a physical (device-oriented)
file name.
4-3
Review of First Three Divisions
• DATA DIVISION
– FILE SECTION
• Detailed description of input/output records
– WORKING-STORAGE SECTION
• Defines keyed input, displayed output
• Defines fields needed for processing but not
part of input/output records
4-4
PROCEDURE DIVISION
Contains instructions to
• Read data
• Process data
• Produce output
• Perform end-of-job operations
4-5
PROCEDURE DIVISION
• Interactive processing instructions
– Accept input from keyboard
– Display output on screen
• Batch processing instructions
– Access files and read them
– Write output to files
4-6
Paragraphs
• PROCEDURE DIVISION divided into
paragraphs
• Each is independent module or routine
• I will use these terms interchangeably.
• Made up of series of instructions to
perform specific set of operations
4-7
Rules for Paragraph-Names
• Coded in Area A, followed by period
• Follow rules for forming data-names
except may be all digits
– 1010, 1020, 1030, etc. are valid paragraph
names
– We will use combinations of digits and
letters as will be appropriate.
• Must be unique
4-8
Rules for Paragraph-Names
• Examples – of ‘high level modules’
– 0000-Main
– 1000-initialize
– 2000-process
– 3000-terminate
– x000-edit
– x000-update, etc.
– At the main module level, we will use
paragraphs in the thousands….
4-9
Procedure Division Statements
• All statements coded in Area B
– Start in position 12.
• Statement begins with verb (READ,
MOVE)
• Last statement in paragraph ends
with period
• Sentence - series of statements ending
with period
4-10
Batch Program Instructions
• OPEN - to open files to be processed
– “Connect” with files (logical to physical)
• PERFORM UNTIL … END-PERFORM
– Loop to continually READ and process
input records and WRITE results to output
file until there are no more records
• CLOSE - to close files when done processing
• STOP RUN - to end program
4-11
OPEN Statement
• Accesses and makes files available for
processing
• Identifies whether files will be used for
input or output
• If the machine cannot ‘find’ a file, you
will get a notice. Check spellings
and path!!! This is an easy error to
get.
4-12
OPEN Statement
FORMAT
OPEN
Explain this syntax!!
INPUT
file-name-1 …
OUTPUT file-name-2 …
• File-names used must appear in SELECT
statement
• File must be accessed with OPEN before
reading from or writing to it
• as we have said a number of times.
4-13
OPEN statement example
• May use a single OPEN statement
 Open Input Payroll-File
Output PayChecks, Err-List
• May use multiple OPEN statements
Open Input Payroll-File
Open Output PayChecks
Open Output Err-List
4-14
PERFORM UNTIL … END-PERFORM
FORMAT
Explain syntax!!
PERFORM UNTIL condition-1
statement-1 ...
[END-PERFORM]
Note: scope terminator
• Repeatedly executes statement(s)
between PERFORM UNTIL … ENDPERFORM until condition specified in
UNTIL clause is met
4-15
PERFORM UNTIL … END-PERFORM
• In typical batch program, instructions
repeated until no more records in file
• When condition met, program continues with
statement following END-PERFORM
4-16
PERFORM UNTIL … END-PERFORM
EXAMPLE
Note: indentation, scope terminators,
‘pre-condition,’ and ‘post-condition.’
Perform Until WS-More-Data = 'NO'
Read Payroll-File
At End
Move 'NO' To WS-More-Data
Not At End
Perform 200-Process-Record
End-Read
End-Perform
4-17
PERFORM UNTIL … END-PERFORM
• Assume WS-More-Data initially = 'YES'
– Condition not met, so statements in PERFORM
loop will be executed
• WS-More-Data set to 'NO' when MOVE
statement executed
– When condition checked again loop ends since
condition has been met
• I prefer f-eof (flag, end of file) set to 0 initially
via a value statement. At End? Move 1 to feof. Then, Perform … Until F-EOF=1.
• This is merely a stylistic choice. You may
choose what you wish, but use the ‘f’ prefix. 4-18
READ Statement
• Reads one record from file opened for
input each time Read statement is
executed.
• Transfers record to input storage area
– Actually, data is made available via the 01
entry.
• Makes one record available at a time,
not entire file
4-19
READ Statement
•
FORMAT
Explain the syntax! What is required? not?
READ file-name-1
AT END statement-1 …
[NOT AT END statement-2 …]
[END-READ]
•  File-name appears in SELECT
statement, FD entry and OPEN
• AT END tests if there are more records
4-20
READ Statement
• If no more records
– Executes statement(s) after AT END
– Typically statement(s) to cause loop containing
READ to end  ONCE THE NEXT TEST VIA
THE PERFORM IS EXECUTED! THE READ
DOES NOT CAUSE THE LOOP TO TERMINATE.
THE PERFORM DOES!!
• If more records
–
–
–
–
Reads in next record
Executes statement(s) after NOT AT END
Typically statement(s) to process record just read
No. Poor style. Abstraction!! Execute a couple of
statements; if more, Perform them via paragraph!
4-21
Simple PERFORM
FORMAT
Syntax? This is one form of the Perform.
PERFORM paragraph-name
• To execute instructions in separate
paragraph or module one time
• Transfers control to named paragraph
• Executes all instructions in paragraph
• Control returns to statement following
PERFORM
4-22
Out-Of-Line PERFORM
FORMAT
PERFORM paragraph-name
UNTIL condition
• Repeats paragraph until condition met
• Control transfers to named paragraph
• Executes instructions in paragraph and
returns
• May loop over and over until ‘true.’
4-23
Out-Of-Line PERFORM - example
…
perform 2000-Process.
<more statements>
2000-Process.
<statements>
3000-Next-Paragraph…
4-24
In-Line PERFORM
FORMAT
PERFORM UNTIL condition
statement(s)
END-PERFORM
• Repeats statement(s) until condition met
• Statement(s) to be executed are in-line, not in
separate paragraph
• Statements follow the Perform until ‘header’
and stop prior to the End-Perform scope
4-25
terminator.
In-Line PERFORM
Perform until amount = 45
<statements>
<more statements>
end-perform.
no ‘branch’
may be preferable or not – depending on
the volume of statements and level of
abstraction desired.
4-26
End-Of-Job Processing
• Steps performed after all records
processed
• Release all files
• Terminate processing
• Often (not always) done in a paragraph
by itself – high level module.
4-27
CLOSE statement
•
FORMAT
CLOSE file-name-1 ...
• Close all files opened
• Indicates files no longer needed for
processing
• Releases files and deactivates devices
• Always close your files explicitly.
4-28
Closing multiple files
•  May use a single CLOSE statement
Close Payroll-File
PayChecks align these
Err-List
• May use multiple CLOSE statements
Close Payroll-File
Close PayChecks
Close Err-List
4-29
STOP RUN
• Terminates the program
– last ‘executable’ statement in program
– last ‘logical’ statement (not physical) in
pgm.
• Usually last instruction in main module
• Execution continues with next
paragraph if STOP RUN is omitted
– Yes, possible error!
4-30
Interactive Program Statements
• DISPLAY to prompt for input
• ACCEPT to store input in WORKINGSTORAGE areas
• Various statements to process input
• DISPLAY to show output
• DISPLAY to ask if there is more input
• ACCEPT to get response
4-31
Interactive Program Statements
• Statements coded in an in-line
PERFORM loop
• Repeated until user responds that there
is no more input. Use appropriate flag.
• Like batch programs, loop may include
simple PERFORM to execute instructions
for processing input that are in a
separate paragraph
4-32
MOVE statement
FORMAT
Syntax???
MOVE Identifier-1 TO identifier-2
• Copies (not moves) contents of
identifier-1 to identifier-2
• Many forms of the ‘move’
4-33
WRITE statement
•
FORMAT
WRITE record-name-1
• Transmits data from output record area
to associated file on device specified
• Record-name is 01 level name following
FD for a file opened for output
• READ file-name, WRITE record-name
4-34
Comments in COBOL
• Start with asterisk (*) in column 7
• Use as reminders and explanations of
processing performed by program
• Use to describe program in
IDENTIFICATION DIVISION
• Use to describe each module in
PROCEDURE DIVISION
• We use ‘flower boxes.’ See Standards.
4-35
Batch Programs Review
• Programs that process large quantity of
data stored in files
• Require ENVIRONMENT DIVISION
• Require FILE SECTION in DATA
DIVISION
• Use OPEN, READ, WRITE, CLOSE
verbs to process files
4-36
Interactive Programs Review
• Programs that process individual items
rather than large group of items
• Provide quick solution to ACCEPT data,
process it and DISPLAY results
4-37
Chapter Summary
• Batch Program Structure
– Main module's in-line PERFORM UNTIL
repeatedly executes READ statement
– READ gets and processes next record until
there are no more
– Files are opened before PERFORM loop
and closed after loop ends
4-38
Chapter Summary
• Interactive Program Structure
– Main module's in-line PERFORM UNTIL
repeatedly
• Prompts user for input and accepts it
• PERFORMs other paragraphs as needed to
process input
• Asks user if there is more input
– Loop repeats until user indicates there is
no more data
4-39
Chapter Summary
• Paragraph-names coded in Area A and
end with period
• All other PROCEDURE DIVISION
statements coded in Area B
• Statements executed in order unless
– PERFORM UNTIL loop executes
– PERFORM transfers control to another
paragraph
4-40
Copyright © 2003 John Wiley & Sons, Inc. All rights reserved.
Reproduction or translation of this work beyond that permitted in Section
117 of the 1976 United States Copyright Act without the express written
permission of the copyright owner is unlawful. Request for further
information should be addressed to the Permissions Department, John
Wiley & Sons, Inc. The purchaser may make back-up copies for his/her
own use only and not for distribution or resale. The Publisher assumes no
responsibility for errors, omissions, or damages, caused by the use of these
programs or from the use of the information contained herein.
4-41