Chapter 2: Key Takeaways -- COBOL Program Structure
Chapter Summary
Chapter 2 establishes the architectural foundation of every COBOL program: the four-division structure. This structure -- IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE -- is not merely a convention but a language requirement that enforces a clean separation of concerns between program identity, platform configuration, data definitions, and executable logic. Understanding this blueprint is essential because every COBOL program ever written, from ten lines to ten thousand, follows this same fundamental organization.
The chapter also introduces the fixed reference format inherited from the punched-card era, where specific column ranges carry specific meanings. Area A (columns 8-11) holds division headers, section headers, paragraph names, and level indicators like 01 and 77. Area B (columns 12-72) holds statements, clauses, and subordinate data definitions. While free format (introduced in COBOL-2002) removes these column restrictions, fixed format remains dominant in production mainframe environments and is the primary format used throughout this textbook.
Beyond the structural rules, the chapter covers the PROCEDURE DIVISION's organizational hierarchy of sections, paragraphs, sentences, and statements, along with the critical role of the period as a scope delimiter. Modern best practice is to minimize periods (one per paragraph) and use explicit scope terminators like END-IF and END-PERFORM to avoid the subtle bugs that misplaced periods cause. Naming conventions using numeric prefixes (0000-MAIN, 1000-INITIALIZE, 2000-PROCESS) and descriptive data name prefixes (WS- for WORKING-STORAGE, LS- for LOCAL-STORAGE) round out the professional practices every COBOL programmer needs.
Key Concepts
- Every COBOL program must contain four divisions in this exact order: IDENTIFICATION, ENVIRONMENT, DATA, and PROCEDURE. Only IDENTIFICATION is strictly required.
- The IDENTIFICATION DIVISION names the program via PROGRAM-ID, which is the only required paragraph in the entire program. Optional paragraphs (AUTHOR, DATE-WRITTEN, etc.) are obsolete in the standard but still widely used in production shops.
- The ENVIRONMENT DIVISION bridges the program to the operating system, with CONFIGURATION SECTION for compiler settings and INPUT-OUTPUT SECTION for file assignments via SELECT statements.
- The DATA DIVISION defines all data the program uses, including FILE SECTION (record layouts), WORKING-STORAGE SECTION (persistent variables), LOCAL-STORAGE SECTION (per-invocation variables), and LINKAGE SECTION (parameters from calling programs).
- The PROCEDURE DIVISION contains executable logic organized into sections, paragraphs, sentences, and statements.
- Fixed reference format divides each line into six areas: Sequence Number (1-6), Indicator (7), Area A (8-11), Area B (12-72), and Identification (73-80).
- Column 7 controls line behavior: space for normal code, asterisk for comments, hyphen for continuation, and D for debugging lines.
- Free format removes column restrictions, uses
*>for inline comments, and allows lines up to 255 characters. - FILE-CONTROL SELECT statements map logical file names to physical files, supporting SEQUENTIAL, INDEXED, RELATIVE, and LINE SEQUENTIAL organizations.
- The FILE STATUS clause stores a two-character code after every file operation and should always be checked for errors.
- Level numbers define data hierarchy: 01 for records, 02-49 for subordinates, 66 for RENAMES, 77 for independent items, and 88 for condition names.
- Condition names (88-level) associate meaningful names with data values, enabling readable code like
IF END-OF-FILEinstead ofIF WS-EOF-FLAG = 'Y'. - REDEFINES allows multiple data descriptions to share the same storage, functioning like a union.
- Periods terminate sentences and implicitly close all open scope delimiters, making them the most consequential punctuation mark in COBOL.
Common Pitfalls
- Wrong division order. The compiler rejects any order other than IDENTIFICATION, ENVIRONMENT, DATA, PROCEDURE. This is not a guideline -- it is a language requirement.
- Area A vs. Area B violations. Division headers, section headers, paragraph names, and level 01/77 must start in Area A (columns 8-11). Statements and subordinate level numbers (02-49, 66, 88) must start in Area B (columns 12-72). Putting them in the wrong area causes compiler errors.
- Misplaced periods terminating IF blocks early. A period inside an IF block terminates the IF statement, causing subsequent statements to execute unconditionally. Use END-IF instead of relying on periods.
- Code extending past column 72. In fixed format, anything after column 72 is silently ignored by the compiler. Long literals or statements that cross this boundary are truncated without warning.
- Using reserved words as data names. COBOL has over 400 reserved words. Common traps include DATE, TIME, STATUS, COUNT, and LENGTH. The WS- prefix convention virtually eliminates this problem.
- Forgetting FILE STATUS checks after file operations. Without FILE STATUS, file errors may go undetected, causing silent data corruption or misleading results.
- Level 01 items placed in Area B or level 05 items placed in Area A. Levels 01 and 77 must start in Area A; levels 02-49, 66, and 88 must start in Area B.
- Missing periods after division headers, section headers, and paragraph names. These structural elements all require a terminating period.
Quick Reference
COBOL Program Skeleton:
IDENTIFICATION DIVISION.
PROGRAM-ID. program-name.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO "physical-name"
ORGANIZATION IS LINE SEQUENTIAL
FILE STATUS IS ws-status.
DATA DIVISION.
FILE SECTION.
FD file-name.
01 record-name.
05 field-name PIC X(20).
WORKING-STORAGE SECTION.
01 ws-variable PIC 9(5) VALUE ZEROS.
PROCEDURE DIVISION.
0000-MAIN.
PERFORM 1000-INITIALIZE
PERFORM 2000-PROCESS UNTIL END-OF-FILE
PERFORM 3000-FINALIZE
STOP RUN.
Column Layout (Fixed Format):
Cols 1-6: Sequence number (ignored by compiler)
Col 7: Indicator (* = comment, - = continuation, D = debug)
Cols 8-11: Area A (headers, paragraph names, 01/77 levels)
Cols 12-72: Area B (statements, subordinate levels)
Cols 73-80: Identification area (ignored by compiler)
Scope Terminators:
END-IF, END-EVALUATE, END-PERFORM, END-READ, END-WRITE,
END-COMPUTE, END-STRING, END-CALL, END-ADD, END-SUBTRACT,
END-MULTIPLY, END-DIVIDE, END-SEARCH, END-UNSTRING
File Status Codes:
"00" = Success "10" = End of file "23" = Record not found
"22" = Dup key "30" = I/O error "35" = File not found
What's Next
Chapter 3 dives deep into COBOL's unique data description system -- the PICTURE clause. You will learn how to define numeric, alphanumeric, and alphabetic data items character by character, control internal storage with the USAGE clause (DISPLAY, COMP, COMP-3), format numbers for output with editing pictures, and use figurative constants like ZEROS, SPACES, and HIGH-VALUES. The PICTURE clause is the heart of COBOL's DATA DIVISION, and mastering it is essential for every chapter that follows.