PERFORM
The COBOL statement that runs a block of code and comes back. It is how loops are written.
Also written PERFORM UNTIL, PERFORM VARYING
Why it matters. Posting every payment in a file means doing the same steps again and again. PERFORM does that.
See it
A paragraph name starts in area A.
Syntax
PERFORM n TIMES statements END-PERFORM PERFORM [WITH TEST BEFORE|AFTER] UNTIL condition statements END-PERFORM PERFORM paragraph [THRU paragraph] PERFORM paragraph VARYING counter FROM a BY b UNTIL condition
- TIMES
- Repeat a fixed number of times.
- UNTIL
- Repeat until the condition is true.
- WITH TEST BEFORE / AFTER
- Test before each pass (the default) or after.
- VARYING
- Count a field from a start value in steps.
- THRU
- Run a range of paragraphs in order.
Try it PERFORM
- 1
In the program POST3, post three payments. Write one line that shows POSTING PAYMENT three times, and ends the loop.
COBOL PERFORM 3 TIMES DISPLAY 'POSTING PAYMENT' END-PERFORM - 2
Run POST3.
RUNCOBOL 'ZB0001.DEV.COBOL(POST3)'
Remember
- PERFORM runs a block of code, then returns to the next line.
- PERFORM n TIMES repeats. PERFORM UNTIL repeats until a test is true, and it tests first unless you write WITH TEST AFTER.
- PERFORM name runs a paragraph or section, which is a named block of code. PERFORM VARYING counts for you.
The usual mistake
PERFORM UNTIL WS-EOF = 'Y' with nothing inside that sets WS-EOF
The test never becomes true, so the loop never ends. The job keeps going until the system's time limit stops it (abend S322) or an operator cancels it.
Put a statement inside the loop that can set WS-EOF to 'Y'
On real z/OS
MF/OS runs the usual forms, including inline PERFORM and VARYING. Its loops have a safety limit that stops a runaway program, which real z/OS does through the job time limit.
Where can I practise PERFORM?
Right here, at ZedBank. You take a junior job at a simulated bank and use PERFORM in real work, with Zed beside you. There's nothing to install.
Start at ZedBankRelated
- PROCEDURE DIVISIONThe part of a COBOL program that contains the actual logic, the statements that run.
- EVALUATECOBOL's multi-way decision statement, and a clearer alternative to a long chain of nested IFs.
- COBOLThe programming language most business logic on the mainframe is written in. Verbose by design, and still running the world's payments.
- File descriptionThe FD entry in a COBOL program that describes a file's records, connecting the program to data supplied by the JCL.