The batch program skeleton
Open almost any production batch program and you will find the same shape. It is called structured or top-down design, and it has been the standard since the late 1970s. Learn it once and you can find your way around programs you have never seen before.
Mainline, initialise, process, terminate
PROCEDURE DIVISION.
0000-MAINLINE.
PERFORM 1000-INITIALISE
PERFORM 2000-PROCESS-RECORD UNTIL END-OF-INPUT
PERFORM 3000-TERMINATE
STOP RUN.
The mainline reads like a table of contents. Each paragraph it calls has one job:
| Paragraph | Job |
|---|---|
1000-INITIALISE |
Open files, clear counters, print headings, priming read |
2000-PROCESS-RECORD |
Handle one record, then read the next |
3000-TERMINATE |
Print totals, close files, set the return code |
8000-READ-... |
Read one record and set the end-of-input flag |
Here is a complete runnable example that counts and totals payments until it reads a zero amount:
IDENTIFICATION DIVISION.
PROGRAM-ID. PAYSUM.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-AMOUNT PIC 9(5)V99.
01 WS-COUNT PIC 9(3) VALUE ZERO.
01 WS-TOTAL PIC 9(7)V99 VALUE ZERO.
01 WS-EOF-FLAG PIC X VALUE "N".
88 END-OF-INPUT VALUE "Y".
PROCEDURE DIVISION.
0000-MAINLINE.
PERFORM 1000-INITIALISE
PERFORM 2000-PROCESS-PAYMENT UNTIL END-OF-INPUT
PERFORM 3000-TERMINATE
STOP RUN.
1000-INITIALISE.
DISPLAY "PAYMENT SUMMARY"
PERFORM 8000-READ-PAYMENT.
2000-PROCESS-PAYMENT.
ADD 1 TO WS-COUNT
ADD WS-AMOUNT TO WS-TOTAL
PERFORM 8000-READ-PAYMENT.
3000-TERMINATE.
DISPLAY "PAYMENTS: " WS-COUNT
DISPLAY "TOTAL: " WS-TOTAL.
8000-READ-PAYMENT.
ACCEPT WS-AMOUNT
IF WS-AMOUNT = ZERO
SET END-OF-INPUT TO TRUE
END-IF.
With input 125, 75, 0 it prints:
PAYMENT SUMMARY
PAYMENTS: 002
TOTAL: 0000200.00
Notice the priming read from the last lesson, split across paragraphs:
1000-INITIALISE reads the first record, and 2000-PROCESS-PAYMENT reads
the next one as its last action. When 8000-READ-PAYMENT sees the end
of the input, it sets the flag, and the UNTIL in the mainline stops the loop
before the terminator is processed.
Why this shape wins
- One reading per record, one place. All input goes through one read paragraph. When the input moves from the terminal to a file, or gains a validation step, you change it in one place.
- Each paragraph fits on a screen. Mainframe editors (ISPF) show about 50 lines. Short paragraphs with clear names are what make a 20,000-line program maintainable.
- Predictable numbering.
1000sets up,2000does the work,3000finishes,8000/9000are shared utilities such as reads and error handling. Sub-steps get sub-numbers:2100-VALIDATE,2200-CALCULATE.
On the job
When you are handed an unfamiliar program, read the mainline first, then
the 1000 paragraph, then the read paragraph. Within five minutes you
will know what the program consumes and what it produces. Many shops
keep a skeleton program to copy for every new batch job.
Your task
The warehouse sends order lines as fixed-layout records, one per line,
ending with a line containing END:
| Columns | Field | Picture |
|---|---|---|
| 1–6 | Item code | X(6) |
| 7–9 | Quantity | 9(3) |
| 10–15 | Unit price, 2 implied decimals | 9(4)V99 |
So NUT002012000015 is item NUT002, quantity 12, price 0.15.
Produce this report:
ORDER REPORT
BLT001 5 X 1.25 = 6.25
NUT002 12 X 0.15 = 1.80
WSH003 100 X 0.05 = 5.00
LINES: 003
TOTAL: 13.05
The starter declares all the fields, including edited output fields, and
provides 8000-READ-ORDER, which reads one line and sets END-OF-INPUT.
Fill in the classic skeleton: 0000-MAINLINE performs
1000-INITIALISE, then 2000-PROCESS-LINE until end of input, then
3000-TERMINATE.