Exercises: COBOL Program Structure Deep Dive
Exercise 3.1: Column Convention Drill
Without using a compiler, identify the error in each of the following COBOL source lines. State which column rule is violated.
a)
01 WS-COUNTER PIC 9(5).
MOVE 0 TO WS-COUNTER.
IDENTIFICATION DIVISION.
b)
PROGRAM-ID. MY-PROGRAM.
c)
IF WS-AMOUNT > 100 * This is a comment
ADD 1 TO WS-COUNT
END-IF.
After identifying the errors, write the corrected versions.
Exercise 3.2: IDENTIFICATION DIVISION Completion
Given the following incomplete IDENTIFICATION DIVISION, add appropriate entries for all standard paragraphs and a comprehensive comment header following GlobalBank coding standards:
IDENTIFICATION DIVISION.
PROGRAM-ID. CLM-ADJUD.
Include a change log with at least two fictional entries. The program is a MedClaim claims adjudication module.
Exercise 3.3: SPECIAL-NAMES Usage
Write a complete COBOL program that demonstrates:
a) DECIMAL-POINT IS COMMA — Display a monetary amount in European format b) A user-defined CLASS for validating that a field contains only digits and hyphens c) CURRENCY SIGN IS "GBP" WITH PICTURE SYMBOL "G" — Display an amount in British pounds
The program should display all three formatted values with labels.
Exercise 3.4: DATA DIVISION Architecture
Design the complete DATA DIVISION (FILE SECTION and WORKING-STORAGE SECTION) for a program that:
- Reads a sequential transaction file (record length 150 bytes) containing employee pay records
- Writes a sequential payroll report (record length 132 bytes)
- Calculates gross pay, tax deductions, and net pay
- Tracks totals for all employees processed
Include: FD entries, 01-level records with detailed subordinate items, level 88 condition names for employee types and status codes, formatted output fields, counters, flags, and file status fields. Follow the organization patterns from Section 3.4.
Exercise 3.5: Copybook Design
Design a set of three copybooks for MedClaim's system:
a) CLM-REC.cpy — A claims record layout with at least 15 fields including claim number, member ID, provider ID, diagnosis codes, procedure codes, amounts, and status
b) MBR-REC.cpy — A member (insured person) record with at least 12 fields
c) CLM-STATUS.cpy — A set of level 88 condition names for claim status values (received, pending, approved, denied, paid, appealed, etc.)
Write a short COBOL program fragment showing how all three copybooks would be used together in a claims processing program, including use of COPY REPLACING.
Exercise 3.6: Anti-Pattern Repair
The following program contains multiple structural anti-patterns. Identify each anti-pattern, explain why it is problematic, and rewrite the program following production coding standards:
IDENTIFICATION DIVISION.
PROGRAM-ID. BAD-EXAMPLE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT F1 ASSIGN TO INFILE.
SELECT F2 ASSIGN TO OUTFILE.
DATA DIVISION.
FILE SECTION.
FD F1.
01 R1.
05 A PIC X(10).
05 B PIC 9(5).
05 C PIC X(85).
FD F2.
01 R2 PIC X(100).
WORKING-STORAGE SECTION.
77 X PIC X VALUE "N".
77 Y PIC 9(5) VALUE 0.
PROCEDURE DIVISION.
P1.
OPEN INPUT F1 OUTPUT F2.
READ F1 AT END MOVE "Y" TO X.
PERFORM UNTIL X = "Y"
ADD 1 TO Y
IF B > 10000
WRITE R2 FROM R1.
READ F1 AT END MOVE "Y" TO X
END-PERFORM.
DISPLAY "RECORDS: " Y.
CLOSE F1 F2.
STOP RUN.
Exercise 3.7: Program Skeleton Generator
Write a complete COBOL program called SKELETON that, when run, DISPLAYs to the console a skeleton COBOL program following GlobalBank coding standards. The skeleton should include all four divisions with common sections, placeholder paragraphs (000-MAIN, 100-INITIALIZE, 200-PROCESS, 300-FINALIZE), and example working storage groups. This is a meta-exercise: you are writing COBOL that generates COBOL.
Exercise 3.8: Comprehensive Structure Analysis
Obtain a sample COBOL program from an open-source repository (such as the Open Mainframe Project's COBOL course materials) and write a detailed structural analysis. Your analysis should:
a) Identify every division, section, and paragraph b) Map the data hierarchy in the DATA DIVISION (draw a tree showing group/elementary relationships) c) Trace the flow of control through the PROCEDURE DIVISION (which paragraphs call which) d) Identify which coding standards from Section 3.9 the program follows and which it violates e) Suggest three specific improvements to the program's structure