From GO TO to structured COBOL
Early COBOL programs were organised the way early programs in every language were: as a sequence of labelled blocks connected by jumps. A paragraph did some work and ended with GO TO the next paragraph, or with a test that jumped somewhere else. Programs grew to tens of thousands of lines, and following the flow of control meant tracing jumps across the listing with a finger. Maintenance programmers called it spaghetti.
In 1968 Edsger Dijkstra argued that unrestricted jumps make programs hard to reason about, and the structured-programming movement that followed reached COBOL in force with the 1985 standard. It added scope terminators (END-IF, END-PERFORM and friends), the inline PERFORM ... END-PERFORM, and EVALUATE. With them, a COBOL program can be written as a hierarchy of paragraphs, each doing one job and each reached only by PERFORM, with no GO TO at all.
You will read both kinds of code in your career. This lesson teaches the structured style you should write, and enough of the older style to read it safely.
The pieces of the procedure division
Definition— Paragraph
A named block of statements in the procedure division. Its name stands alone on a line, followed by a full stop, and the paragraph runs until the next paragraph name. A section is a named group of paragraphs (name SECTION.); many shops use sections only where a standard or tool requires them.
Definition— PERFORM
The statement that runs a paragraph (or an inline block) and then returns to the statement after the PERFORM. It is COBOL's procedure call and its loop: there is no separate while or for statement.
The forms of PERFORM you will use most:
| Form | Meaning |
|---|---|
PERFORM para | Run para once and come back |
PERFORM para 3 TIMES | Run it a fixed number of times |
PERFORM para UNTIL cond | Test cond; if false, run para; repeat. May run zero times |
PERFORM para WITH TEST AFTER UNTIL cond | Run first, then test: always at least once |
PERFORM para VARYING i FROM a BY b UNTIL cond | A counting loop; add AFTER j FROM ... for a nested loop |
PERFORM ... END-PERFORM | Any of the above with the statements written inline instead of in a paragraph |
PERFORM para-a THRU para-z | Run a range of consecutive paragraphs (older style) |
Note that UNTIL is the condition for stopping, the opposite of a while loop in C or Python. PERFORM ... UNTIL balance > 0 loops while the balance is not yet positive.
Two statements leave early: EXIT PERFORM leaves the innermost inline PERFORM, and EXIT PARAGRAPH leaves the current paragraph (both from COBOL 2002). CONTINUE does nothing and is useful where the syntax demands a statement. STOP RUN ends the whole run; GOBACK returns to whoever called this program, which for a main program is the operating system.
Conditions
| Kind | Examples |
|---|---|
| Relational | a = b, a > b, a NOT = b, a >= b, a IS GREATER THAN b |
| Class | amount IS NUMERIC, name IS ALPHABETIC |
| Sign | balance IS NEGATIVE, delta IS ZERO, total IS POSITIVE |
| Condition name | target-reached, NOT end-of-file (an 88 level) |
| Combined | a > 0 AND b > 0, NOT (x = 1 OR y = 2) |
COBOL also has abbreviated combined conditions: IF day-number = 6 OR 7 means IF day-number = 6 OR day-number = 7. They read naturally but can surprise you when NOT is involved; when in doubt, write the full form or use an 88 level.
IF and EVALUATE
IF condition ... ELSE ... END-IF works as in any language. EVALUATE is COBOL's multi-way decision, much richer than a C switch:
EVALUATE TRUE
WHEN balance < 0 PERFORM 3100-overdrawn
WHEN balance < 100 PERFORM 3200-low-balance
WHEN OTHER CONTINUE
END-EVALUATEEVALUATE TRUE tries each WHEN condition in order and runs the first one that is true; there is no fall-through to the next WHEN. You can also evaluate a value against ranges (WHEN 0 THRU 999) and several subjects at once with ALSO, which turns a decision table into code.
Programs built from paragraphs
A main paragraph and its workers
This program works out how many months of saving 250.00 at 0.4% interest a month it takes to reach 3,000.00. Notice its shape: the first paragraph reads like a table of contents, and each worker paragraph does one job.
IDENTIFICATION DIVISION.
PROGRAM-ID. savings-plan.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 ws-balance PIC S9(7)V99 VALUE 0.
01 ws-deposit PIC S9(5)V99 VALUE 250.00.
01 ws-target PIC S9(7)V99 VALUE 3000.00.
01 ws-monthly-rate PIC V9(6) VALUE .004.
01 ws-interest PIC S9(5)V99.
01 ws-month PIC 999 VALUE 0.
01 ws-month-out PIC ZZ9.
01 ws-money-out PIC Z,ZZZ,ZZ9.99.
01 ws-status PIC X VALUE "N".
88 target-reached VALUE "Y".
PROCEDURE DIVISION.
0000-main.
PERFORM 1000-initialise
PERFORM 2000-add-one-month UNTIL target-reached
PERFORM 9000-report
STOP RUN.
1000-initialise.
MOVE ws-target TO ws-money-out
DISPLAY "Target: " ws-money-out.
2000-add-one-month.
ADD 1 TO ws-month
COMPUTE ws-interest ROUNDED = ws-balance * ws-monthly-rate
ADD ws-interest ws-deposit TO ws-balance
IF ws-balance >= ws-target
SET target-reached TO TRUE
END-IF.
9000-report.
MOVE ws-month TO ws-month-out
MOVE ws-balance TO ws-money-out
DISPLAY "Months: " ws-month-out
DISPLAY "Balance: " ws-money-out.Target: 3,000.00
Months: 12
Balance: 3,066.87Points of style that experienced COBOL programmers will recognise:
- Numbered paragraph names.
0000-main,1000-initialise,2000-...,9000-report. The numbers give the order of the hierarchy and make paragraphs easy to find in a long listing; the words say what each does. Conventions vary by shop, but some convention is universal. - The loop control is a condition name.
PERFORM 2000-add-one-month UNTIL target-reachedreads as a sentence, and the paragraph sets the flag withSET target-reached TO TRUE. Nobody needs to know that the flag is the letterY. - One full stop per paragraph, at the end. Inside the paragraph,
END-IFcloses theIF. ADD a b TO cadds several values at once.STOP RUNends0000-main. Without it, control would fall into the next paragraph, as we will see.
EVALUATE as a decision table
A courier prices parcels by zone and weight. The business rule is a table, and EVALUATE ... ALSO lets the code look like the table:
IDENTIFICATION DIVISION.
PROGRAM-ID. shipping.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 parcel.
05 zone PIC X.
88 domestic VALUE "D".
88 regional VALUE "R".
88 international VALUE "I".
05 grams PIC 9(5).
01 price PIC 999V99.
01 price-out PIC ZZ9.99.
PROCEDURE DIVISION.
0000-main.
MOVE "D00500" TO parcel PERFORM 1000-price-parcel
MOVE "D02500" TO parcel PERFORM 1000-price-parcel
MOVE "R01200" TO parcel PERFORM 1000-price-parcel
MOVE "I00800" TO parcel PERFORM 1000-price-parcel
MOVE "I03100" TO parcel PERFORM 1000-price-parcel
MOVE "X00100" TO parcel PERFORM 1000-price-parcel
STOP RUN.
1000-price-parcel.
EVALUATE TRUE ALSO grams
WHEN domestic ALSO 0 THRU 1000
MOVE 4.50 TO price
WHEN domestic ALSO ANY
MOVE 9.00 TO price
WHEN regional ALSO 0 THRU 2000
MOVE 12.00 TO price
WHEN international ALSO 0 THRU 2000
MOVE 25.00 TO price
WHEN international ALSO ANY
COMPUTE price = 25.00 + (grams - 2000) / 1000 * 8
WHEN OTHER
MOVE ZERO TO price
END-EVALUATE
IF price = ZERO
DISPLAY zone " " grams "g no such zone"
ELSE
MOVE price TO price-out
DISPLAY zone " " grams "g " price-out
END-IF.D 00500g 4.50
D 02500g 9.00
R 01200g 12.00
I 00800g 25.00
I 03100g 33.80
X 00100g no such zoneEach WHEN has one entry per subject, separated by ALSO. The first subject is TRUE, so its entries are conditions (domestic); the second is grams, so its entries are values or ranges (0 THRU 1000), or ANY to match anything. The order matters: a domestic parcel of 2,500 g fails the first row (0 THRU 1000) and matches the second (ANY). The international rate charges 8.00 per extra kilogram beyond 2 kg, so 3,100 g costs 25.00 + 1.1 × 8.00 = 33.80. WHEN OTHER catches the unknown zone X, which is exactly the kind of bad input real data contains.
Also notice MOVE "D00500" TO parcel: one move into the group fills both fields, as in lesson 2. Reading a record from a file does the same.
Inline loops, nested loops and early exit
IDENTIFICATION DIVISION.
PROGRAM-ID. loops.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 r PIC 9.
01 c PIC 9.
01 cell PIC Z9.
01 n PIC 999.
01 attempts PIC 9 VALUE 0.
01 day-number PIC 9.
PROCEDURE DIVISION.
main-para.
PERFORM VARYING r FROM 1 BY 1 UNTIL r > 3
PERFORM VARYING c FROM 1 BY 1 UNTIL c > 4
COMPUTE cell = r * c
DISPLAY cell " " WITH NO ADVANCING
END-PERFORM
DISPLAY "|"
END-PERFORM
PERFORM VARYING n FROM 1 BY 1 UNTIL n > 999
IF n * n > 2026
EXIT PERFORM
END-IF
END-PERFORM
DISPLAY "smallest n with n * n > 2026: " n
MOVE 9 TO r
PERFORM WITH TEST AFTER UNTIL r > 5
ADD 1 TO attempts
END-PERFORM
DISPLAY "test-after loop ran " attempts " time(s)"
PERFORM VARYING day-number FROM 5 BY 1 UNTIL day-number > 7
IF day-number = 6 OR 7
DISPLAY "day " day-number ": weekend"
ELSE
DISPLAY "day " day-number ": weekday"
END-IF
END-PERFORM
STOP RUN. 1 2 3 4 |
2 4 6 8 |
3 6 9 12 |
smallest n with n * n > 2026: 046
test-after loop ran 1 time(s)
day 5: weekday
day 6: weekend
day 7: weekendDISPLAY ... WITH NO ADVANCINGstays on the same line, so the inner loop builds a row and the outer loop ends it.EXIT PERFORMleaves the search loop as soon as 46 × 46 = 2116 exceeds 2026 (45 × 45 is 2025, one short).- The test-after loop ran once even though its condition,
r > 5, was already true. IF day-number = 6 OR 7is the abbreviated form ofday-number = 6 OR day-number = 7.
Traps in the procedure division
The full stop that ends an IF
Before 1985 there was no END-IF. An IF ended at the next full stop, and that rule still holds. Compare two versions of the same check when the balance is 120.00, not negative:
IDENTIFICATION DIVISION.
PROGRAM-ID. stray-period.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 balance PIC S9(5)V99 VALUE 120.00.
PROCEDURE DIVISION.
main-para.
DISPLAY "Version 1: scope terminator"
IF balance < 0
DISPLAY " overdrawn"
DISPLAY " fee charged"
END-IF
DISPLAY "Version 2: a full stop too many"
IF balance < 0
DISPLAY " overdrawn".
DISPLAY " fee charged"
DISPLAY "done"
STOP RUN.Version 1: scope terminator
Version 2: a full stop too many
fee charged
doneIn version 2, the full stop after " overdrawn" ended the IF. The indented DISPLAY " fee charged" is no longer inside it, so it runs for everyone. The compiler gave no warning: the program is perfectly legal. Imagine that line being PERFORM 3500-charge-overdraft-fee. This is why modern COBOL style closes every conditional with its scope terminator and puts a single full stop at the end of the paragraph.
Falling through into the next paragraph
Paragraphs are not functions. When a paragraph that was not reached by PERFORM ends, control simply continues into the next paragraph in the source. This program has no STOP RUN:
PROCEDURE DIVISION.
main-para.
PERFORM greet
DISPLAY "main done".
greet.
DISPLAY "hello".It prints hello, main done, and then hello again, because after main-para finishes, execution falls into greet, and only then reaches the end of the program. Always end the main paragraph with STOP RUN or GOBACK.
Misconception
"PERFORM ... UNTIL is a while loop." The condition says when to stop, and by default it is tested before each pass. If it is already true, the body never runs. Add WITH TEST AFTER for a body that must run at least once.
Misconception
"EVALUATE falls through like a C switch." It never does. Exactly one WHEN runs (the first that matches), or WHEN OTHER, or nothing.
Misconception
"PERFORM THRU and GO TO are always wrong." Many older shops use the pattern PERFORM 2000-process THRU 2000-exit, where 2000-exit is an empty paragraph and GO TO 2000-exit is the only jump allowed, as an early return. It is disciplined and you will meet it often. In new code, EXIT PARAGRAPH does the same job without a jump; when you change old code, follow the conventions already in it.
How professionals structure programs
A typical batch program in a bank or insurer, whatever its business purpose, has the same skeleton: initialise (open files, read the first record, set up totals), process records until the input is exhausted, then finalise (print totals, close files). The main paragraph shows exactly that and nothing else. Every other paragraph is reached from above by PERFORM, and none is longer than a screen. Teams typically enforce such rules in code review and with static-analysis tools that flag GO TO, PERFORM THRU across ranges, missing scope terminators and paragraphs that can be fallen into.
Reading the older style
Sooner or later you will open a program written in the 1970s or 1980s, and it will look something like this:
2000-PROCESS-ACCOUNT.
IF ACCT-STATUS = 'C'
GO TO 2000-EXIT.
IF ACCT-BALANCE < ZERO
PERFORM 3100-OVERDRAWN
GO TO 2000-EXIT.
ADD ACCT-BALANCE TO WS-TOTAL.
2000-EXIT.
EXIT.and it will be called with PERFORM 2000-PROCESS-ACCOUNT THRU 2000-EXIT. Read it this way: the range from 2000-PROCESS-ACCOUNT to 2000-EXIT behaves like one procedure, and GO TO 2000-EXIT is an early return. Each IF ends at its full stop, which is why every branch is a single sentence. The empty EXIT statement simply marks the end of the range. The code is safe as long as nobody jumps into the range from outside and nobody inserts a paragraph between the two names, which would quietly become part of the range. Before you change such a program, search the whole listing for every GO TO and PERFORM ... THRU that mentions the paragraphs you are touching; a cross-reference listing from the compiler (cobc -t listing.txt --Xref in GnuCOBOL, or the XREF option on IBM's compiler) shows every reference in one place.
Two more habits help maintainers decades later. First, keep conditions in the data division where they belong: an 88 level such as account-closed documents the code values, and changing a value later means changing one line. Second, prefer EVALUATE over nested IF statements more than two deep: a decision table in code can be checked line by line against the business specification it implements.
Exercises
Try each question before opening the hints. Every answer about program output was checked by compiling and running the program with GnuCOBOL 3.2.
1. Paragraphs, PERFORM and control flow
Warm-upComputationali is PIC S99. How many lines does this loop display?
PERFORM VARYING i FROM 10 BY -3 UNTIL i < 1
DISPLAY i
END-PERFORM
2. Paragraphs, PERFORM and control flow
Warm-upComputationalscore is 85. What does this print?
EVALUATE TRUE
WHEN score >= 50 DISPLAY "pass"
WHEN score >= 80 DISPLAY "distinction"
WHEN OTHER DISPLAY "fail"
END-EVALUATE
3. Paragraphs, PERFORM and control flow
CoreComputationalbalance is −5. Which lines does this print? List them separated by commas.
IF balance < 0
DISPLAY "overdrawn".
DISPLAY "fee charged"
DISPLAY "done"
STOP RUN.
4. Paragraphs, PERFORM and control flow
CoreComputationalk is PIC 9 VALUE 5 and say-it is a paragraph that displays k. How many lines are printed?
PERFORM say-it 3 TIMES
PERFORM say-it UNTIL k > 4
PERFORM say-it WITH TEST AFTER UNTIL k > 4
5. Paragraphs, PERFORM and control flow
CoreComputationalHow many lines does this complete procedure division print?
PROCEDURE DIVISION.
main-para.
PERFORM greet
DISPLAY "main done".
greet.
DISPLAY "hello".
6. Paragraphs, PERFORM and control flow
CoreHumanWhat does IF region-code = "N" OR "S" mean?
7. Paragraphs, PERFORM and control flow
StretchHumanWhich of these are good reasons for modern COBOL style (scope terminators such as END-IF, and one full stop at the end of each paragraph)? Choose all that apply.
Further reading and resources
- GnuCOBOL guides and documentationThe Programmer's Guide describes every form of PERFORM, IF and EVALUATE.
- IBM Enterprise COBOL for z/OS documentationThe language reference covers procedure division structure, conditional expressions and scope terminators.
- Go To Statement Considered Harmful · Edsger W. Dijkstra · 1968The famous letter that launched the structured-programming movement COBOL-85 embraced.