Control Flow in COBOL
Learn conditionals and loops in COBOL - IF/ELSE, 88-level condition names, EVALUATE, and the versatile PERFORM verb using Docker-ready GnuCOBOL examples
Control flow determines the order in which a program’s statements execute - which branches it takes and how many times it repeats work. As an imperative, procedural language, COBOL gives you a familiar set of tools for this: conditionals, multi-way branches, and loops. What makes COBOL distinctive is how it expresses them. True to its business-readable design, the language spells out control flow in English-like sentences, and it bundles almost all iteration into a single, remarkably flexible verb: PERFORM.
In this tutorial you’ll learn how COBOL handles decisions with IF/ELSE/END-IF, how the language’s signature 88-level condition names turn data values into self-documenting tests, how EVALUATE provides a powerful switch/case construct, and how the one PERFORM verb covers counted loops, conditional loops, and calling reusable paragraphs. COBOL has no for or while keyword - everything iterative flows through PERFORM.
All examples use modern free-format syntax and run with GnuCOBOL in Docker, so you can compile and execute each one without installing anything locally.
Conditionals with IF / ELSE / END-IF
The basic decision in COBOL reads almost like a sentence. The END-IF scope terminator marks where the conditional ends - a feature added in COBOL 85 that replaced error-prone period-based scoping.
Create a file named ifelse.cob:
| |
COBOL accepts both symbolic operators (>, <, =, >=, <=) and their English equivalents (GREATER THAN, LESS THAN, EQUAL TO). Combine conditions with AND, OR, and NOT. Always close an IF with END-IF in modern code - it makes nesting unambiguous.
Condition Names with 88-Levels
COBOL’s most idiomatic conditional feature is the 88-level condition name. Instead of comparing a variable to a literal value scattered throughout your code, you give meaningful names to the values a field can hold. This is one of the clearest examples of COBOL’s self-documenting philosophy.
Create a file named condname.cob:
| |
A VALUE on an 88-level lists the values that make the condition true. Testing IF IS-ACTIVE is equivalent to IF WS-STATUS-CODE = "A", but far more readable. The SET IS-SUSPENDED TO TRUE statement works in reverse - it stores "S" into the parent field, so condition names can both read and write meaning.
Multi-Way Branching with EVALUATE
For multiple branches, COBOL provides EVALUATE, the equivalent of a switch/case statement. Its most powerful form is EVALUATE TRUE, which lets each WHEN test an arbitrary condition - perfect for range checks like grading.
Create a file named evaluate.cob:
| |
EVALUATE checks each WHEN in order and runs the first match - there is no fall-through, so no break is needed. WHEN OTHER acts as the default case. Notice the output shows 085 rather than 85: because WS-SCORE is declared PIC 9(3), it always displays three digits with leading zeros.
Loops with the PERFORM Verb
COBOL has no for or while keyword. Instead, the single PERFORM verb handles every kind of loop. The three inline forms below cover the most common needs.
Create a file named loops.cob:
| |
PERFORM n TIMESrepeats a fixed number of times.PERFORM VARYING ... FROM ... BY ... UNTILis COBOL’s counted loop, initializing and stepping a counter automatically.PERFORM UNTILloops while a condition stays false, like awhileloop. By default the test happens before each iteration (WITH TEST BEFORE); addWITH TEST AFTERfor do-while behavior.
Loop Control and Calling Paragraphs
Modern COBOL (2002+, supported by GnuCOBOL) adds loop control with EXIT PERFORM (break out of the loop) and EXIT PERFORM CYCLE (skip to the next iteration - like continue). PERFORM also has a second job: calling a named paragraph as a reusable block of code, which is how COBOL structures its procedural logic.
Create a file named loopctrl.cob:
| |
The loop prints odd numbers, uses EXIT PERFORM CYCLE to skip even ones via the intrinsic FUNCTION MOD, and EXIT PERFORM to break when it reaches 6. Then PERFORM GREETING-PARA runs the named paragraph twice - control returns automatically after the paragraph finishes. This out-of-line PERFORM is the foundation of COBOL’s procedural structure.
Running with Docker
Compile and run each example with GnuCOBOL. The -x flag produces an executable and -free enables free-format source. With -x, the executable takes its name from the source file (so ifelse.cob produces ./ifelse).
| |
Expected Output
ifelse.cob:
Balance exceeds the credit limit
Working-age adult
Age is at least 18
condname.cob:
Account is active
Account is now suspended
evaluate.cob:
Score 085 earns grade B
Performance: Good
loops.cob:
PERFORM TIMES:
Hello from a loop
Hello from a loop
Hello from a loop
PERFORM VARYING:
i = 01
i = 02
i = 03
i = 04
i = 05
Sum of 1 to 5 = 0015
PERFORM UNTIL:
count = 01
count = 02
count = 03
loopctrl.cob:
Odd number: 01
Odd number: 03
Odd number: 05
Stopping at 6
Calling a paragraph:
Greetings from a paragraph
Greetings from a paragraph
Key Concepts
END-IFand scope terminators - Modern COBOL closes conditionals with explicit terminators (END-IF,END-EVALUATE,END-PERFORM), avoiding the period-scoping ambiguities of older code.- 88-level condition names are uniquely COBOL - they attach readable names to data values, so
IF IS-ACTIVEreplacesIF WS-STATUS-CODE = "A"andSET ... TO TRUEassigns those values back. EVALUATEis COBOL’s switch - it has no fall-through, needs nobreak, andEVALUATE TRUElets eachWHENtest a full condition for range logic.- One verb, every loop -
PERFORMhandles counted (n TIMES), iterating (VARYING), and conditional (UNTIL) loops; there is no separatefororwhile. TEST BEFOREvsTEST AFTER-PERFORM UNTILtests before each iteration by default;WITH TEST AFTERgives do-while semantics that always run at least once.- Loop control is modern -
EXIT PERFORM(break) andEXIT PERFORM CYCLE(continue) come from COBOL 2002 and work in GnuCOBOL. PERFORMalso calls paragraphs - the same verb invokes named paragraphs as reusable routines, the backbone of COBOL’s procedural organization.- Numeric display fields show leading zeros - a
PIC 9(3)value of 85 displays as085; the picture clause controls width, not just storage.
Running Today
All examples can be run using Docker:
docker pull esolang/cobol:latest
Comments
Loading comments...
Leave a Comment