PERFORM statement either runs named paragraph logic and returns, or runs inline statements between PERFORM and END-PERFORM. That one verb is behind many batch loops, validation checks, table scans, and small reusable routines in production COBOL programs.What does PERFORM do in COBOL?
PERFORM transfers control to one or more statements and then returns control to the next executable statement. In older paragraph-style COBOL, that usually means PERFORM 2000-VALIDATE-CUSTOMER. In newer structured code, it often means inline logic ended by END-PERFORM.
This page is the overview. For deep loop-condition detail, use the separate COBOL PERFORM UNTIL guide. For counter-style table loops, use the PERFORM VARYING guide.
Inline PERFORM vs out-of-line PERFORM
An inline PERFORM keeps the statements directly in the current flow. It must end with END-PERFORM. An out-of-line PERFORM names a paragraph or section, branches to it, and returns when that performed range finishes.
| Type | How it is coded | Good fit |
|---|---|---|
| Inline PERFORM | PERFORM ... END-PERFORM |
Short logic that is easier to read in place |
| Out-of-line PERFORM | PERFORM paragraph-name |
Reusable logic or longer processing steps |
Basic PERFORM example
A basic PERFORM runs the named paragraph once. This is common in old batch programs where each paragraph has a single job: initialize, read, validate, calculate, write, and close.
PROCEDURE DIVISION.PERFORM 1000-INITIALIZEPERFORM 2000-PROCESS-FILEPERFORM 9000-CLOSE-FILESGOBACK.1000-INITIALIZE.OPEN INPUT INFILEOUTPUT OUTFILE.
Keep out-of-line paragraphs outside accidental fall-through paths. A paragraph that sits directly after the main flow can run once by PERFORM and again by ordinary sequential flow if the program is structured badly.
Inline PERFORM example
Inline PERFORM is clean for small checks because the reader does not need to jump to another paragraph. It also makes scope clear when paired with END-PERFORM.
PERFORMIF WS-AMOUNT > ZEROADD WS-AMOUNT TO WS-TOTALELSEADD 1 TO WS-ERROR-COUNTEND-IFEND-PERFORM
Use explicit scope terminators such as END-IF inside inline logic. That helps avoid bugs when another condition or loop is added later.
PERFORM TIMES example
PERFORM TIMES runs logic a fixed number of times. IBM documents that if the count is zero or negative when the statement starts, control passes to the statement after the PERFORM.
PERFORM 5 TIMESDISPLAY "RETRYING FILE OPEN"END-PERFORM
This form fits retry loops, small counters, test data generation, and repeatable validation. If the loop needs to stop based on a condition, use PERFORM UNTIL instead.
PERFORM UNTIL example
PERFORM UNTIL repeats while a condition is false, then stops when the condition becomes true. If neither TEST BEFORE nor TEST AFTER is specified, COBOL assumes TEST BEFORE.
PERFORM UNTIL END-OF-FILEREAD INFILEAT ENDSET END-OF-FILE TO TRUENOT AT ENDPERFORM 3000-PROCESS-RECORDEND-READEND-PERFORM
For file processing, always make the loop exit visible. A missing AT END action or a flag that never changes can create an endless loop in a batch job.
PERFORM VARYING example
PERFORM VARYING changes a counter or index each time through the loop. It is common with COBOL tables, especially when checking each occurrence of an array-like structure.
PERFORM VARYING WS-SUB FROM 1 BY 1UNTIL WS-SUB > WS-MAX-ITEMSIF ITEM-CODE(WS-SUB) = SPACESADD 1 TO WS-MISSING-COUNTEND-IFEND-PERFORM
Check the starting value, step value, and stop condition together. If WS-SUB starts at 1 and increases by 1, the condition should eventually become true.
PERFORM THRU warning
PERFORM paragraph-1 THRU paragraph-2 runs a range of paragraphs. It appears in many older systems, but it can be harder to maintain because adding a paragraph inside the range changes what the PERFORM runs. Prefer a single performed paragraph that calls smaller paragraphs when the codebase allows it.
Common mistakes
Recursive PERFORM
A performed paragraph must not cause itself to be performed. IBM warns that recursive PERFORM can produce unpredictable results. In plain terms, do not let 1000-MAIN perform 1000-MAIN again.
Mixing inline and out-of-line syntax
If the PERFORM names a paragraph, do not also put inline imperative statements and END-PERFORM on the same statement. Pick one style for that statement.
Hiding the loop exit
Every loop should show how it ends. For file loops, the AT END path should set a flag. For counter loops, the counter must move toward the end condition.
Related Mainframe Forum guides
Use this page as the PERFORM overview, then read COBOL Basic PERFORM, COBOL PERFORM TIMES, COBOL PERFORM UNTIL, and COBOL PERFORM VARYING. For related control logic, see COBOL IF Statement and COBOL EVALUATE Statement.
External references
IBM documents the formal rules in the Enterprise COBOL PERFORM statement, choosing inline or out-of-line PERFORM, and PERFORM TIMES pages.
FAQ
What is PERFORM in COBOL?
PERFORM runs inline statements or named procedure logic, then returns control to the next executable statement after the performed work finishes.
What is the difference between inline and out-of-line PERFORM?
Inline PERFORM keeps the logic in place and ends with END-PERFORM. Out-of-line PERFORM names a paragraph or section and branches to that procedure.
When should I use PERFORM TIMES?
Use PERFORM TIMES when the number of repeats is known before the loop starts, such as five retry attempts or ten test records.
When should I use PERFORM VARYING?
Use PERFORM VARYING when a counter or index must change on each pass, especially when processing a COBOL table.
No comments:
Post a Comment