Custom Search

mainframe cobol structured designed for both training and reference. The site has downloads available for the chapters,Teaching structured cobol

A First Look at Structured COBOL
COBOL is a structured language. You already have seen some of the rules for layout, but structure goes deeper than just physical layout. Structuring applies to the DATA and the PROCEDURE DIVISIONs of a COBOL program. Today, you learn the structure of the PROCEDURE DIVISION and explore the following topics:
• A new COBOL shell.

• Program flow.

• Paragraph names.

• What is STOP RUN?

• What is the PERFORM verb?

• When to use PERFORM.

• SECTIONs in the PROCEDURE DIVISION.
A New COBOL Shell
From now on, all programs will have some sort of data in them, so it is a good idea to modify cobshl01.cbl, created in Day 1, "Your First COBOL Program." Listing 3.1, cobshl02.cbl, now includes WORKING-STORAGE.
TYPE: Listing 3.1. A new COBOL shell including data.
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. COBSHL02.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500 WORKING-STORAGE SECTION.
000600
000700 PROCEDURE DIVISION.
000800 PROGRAM-BEGIN.
000900
001000 PROGRAM-DONE.
001100 STOP RUN.
Program Flow
The normal course of execution of a COBOL program is from the first statement in the PROCEDURE DIVISION to the last. Let's look at Listing 3.2, add01.cbl, line by line.
TYPE: Listing 3.2. Top-to-bottom execution.
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. ADD03.
000300 ENVIRONMENT DIVISION.
000400 DATA DIVISION.
000500
000600 WORKING-STORAGE SECTION.
000700
000800 01 FIRST-NUMBER PICTURE IS 99.
000900 01 SECOND-NUMBER PICTURE IS 99.
001000 01 THE-RESULT PICTURE IS 999.
001100
001200 PROCEDURE DIVISION.
001300
001400 PROGRAM-BEGIN.
001500 DISPLAY "This program will add 2 numbers.".
001600 DISPLAY "Enter the first number.".
001700
001800 ACCEPT FIRST-NUMBER.
001900
002000 DISPLAY "Enter the second number.".
002100
002200 ACCEPT SECOND-NUMBER.
002300
002400 COMPUTE THE-RESULT = FIRST-NUMBER + SECOND-NUMBER.
002500
002600 DISPLAY "The result is " THE-RESULT.
002700
002800
002900 PROGRAM-DONE.
003000 STOP RUN.
003100
ANALYSIS: The program starts executing at line 001300 in the PROCEDURE DIVISION. Blank lines are skipped, so nothing happens on that line. Line 001400 is a paragraph name. Paragraphs in COBOL are used as bookmarks. The program doesn't do anything for line 001400 except to note internally that it has started a paragraph named PROGRAM-BEGIN.
At line 001500, the program displays a message on-screen. At line 001600, another is displayed. Line 001700 is blank, so it is skipped. At line 001800, the program stops, waits for keyboard input, and places the results in the variable FIRST-NUMBER.
This type of step-by-step action occurs until line 002900, when the program notes that it has begun executing a paragraph named PROGRAM-DONE. At line 003000, the statement STOP RUN is executed, and this halts the execution of the program.