The PROCEDURE DIVISION contains the runnable code of the COBOL source. The instructions and statements are divided into paragraphs which are called procedures here, and which are similar to methods without return values nor argument list in modern languages, or procedures in older languages, again, without their arguments and return values.
A paragraph is marked with a label (a sentence containing the paragraph name only), uses the global variables defined in the DATA DIVISION, and can be executed only once, unless a loop re-uses it.
The instruction pointer (IP) starts on the next sentence after the PROCEDURE DIVISION line, which will be the main paragraph of the program. Showing the label of the main paragraph is optional. When it is shown, it is usually called MAIN-PARA.
The IP steps sequentially downward. If it finds a PERFORM or GO TO, will jump to the marked place, and continues from there.
In case of PERFORM, the IP jumps on the label of the other paragraph. Upon reaching the end of the other paragraph, the IP jumps back to the main paragraph and continues from there.
Putting a set of instructions or paragraphs into a loop makes the program re-use the corresponding instructions but afterward executing the already executed sentences will not be possible.
The execution immediately stops if the IP reaches a STOP RUN. sentence or the end of the source file.
In the PROCEDURE DIVISION, in case the paragraphs are organized into sections, labelling the procedures is still necessary. Also, the whole section can not be performed by using the section name, only the procedure names are valid.
EXAMPLE FOR SYNTAX OF PROCEDURE EXECUTION:
EXAMPLE FOR SEQUENTAL PROCEDURE EXECUTION:
EXAMPLE FOR ITERATED PROCEDURE EXECUTION:
EXAMPLE FOR MULTIPLE PROCEDURE EXECUTION:
EXAMPLE FOR MULTIPLE PROCEDURES AND PROCEDURE EXECUTION:
Data can be received as:
dataset via DD statement (used in the SELECT ... ASSIGN TO ...)
text via in-stream DD (used in the ACCEPT ...)
the PARM execution parameter from EXEC statement in JCL (used in PROCEDURE DIVISION ... USING ...)
See the COBOL FILE MANAGEMENT for the usage of SELECT... ASSIGN TO...
See the JCL EXEC STATEMENT and the COBOL PROGRAM MODULES pages for the usage of USING on the PARM.
See the DD IN-STREAM section on the JCL DD STATEMENTS page for the usage of ACCEPT... on DD in-stream.
See the COBOL PROGRAM MODULES for more.
See the RECEIVING EXECUTION PARAMETER FROM JCL section on the COBOL PROGRAM MODULES page.
The sequential flow can be modified by using
PERFORM (paragraph)
PERFORM UNTIL/VARYING (iteration)
IF/ELSE/ENDIF (conditional)
EVALUATE/WHEN/END-EVALUATE (switch)
GOBACK (return to the caller module)
END PROGRAM (end of module code)
GO TO
EXIT
CONTINUE
See the COBOL ITERATIONS, the COBOL CONDITIONAL STATEMENTS and the COBOL PROGRAM MODULES pages.
The GO TO <paragraph> sentence makes the instruction pointer on the given paragraph. This instruction ruins the control, hardly testable, easily causes infinite loop; can be found in very old codes.
The EXIT has 3 forms:
EXIT (no operation, NOP; placeholder in empty IF/ELSE and EVALUATE/WHEN branches; coming from old COBOL)
EXIT PARAGRAPH (exit the paragraph and return to from where the flow entered the paragraph; in old codes)
EXIT SECTION (exit the section and return to from where the flow entered the section; in old codes)
The CONTINUE is a NOP as well as the EXIT (placeholder) but used in modern COBOL codes.
EXAMPLE FOR USING PLACEHOLDER: