The EXIT PARAGRAPH statement in COBOL is used to explicitly exit a paragraph and return control to the statement following the PERFORM that invoked it. It is rarely needed in modern structured COBOL, but can be useful for early returns in certain cases.
The syntax for EXIT PARAGRAPH is simple:
1EXIT PARAGRAPH.
Place EXIT PARAGRAPH where you want to leave the paragraph early.
12345PROCESS-RECORDS. IF NO-MORE-RECORDS EXIT PARAGRAPH. * ... more logic ... DISPLAY "Processing complete."
If NO-MORE-RECORDS is true, the paragraph exits early.
123456PERFORM PROCESS-RECORDS. ... PROCESS-RECORDS. IF ERROR-FOUND EXIT PARAGRAPH. * ... continue processing ...
EXIT PARAGRAPH returns control to the statement after the PERFORM.
Here are some practical uses of EXIT PARAGRAPH in COBOL programs:
1234VALIDATE-INPUT. IF INVALID-DATA EXIT PARAGRAPH. * ... continue validation ...
Leaves the paragraph if data is invalid.
123456PROCESS-DATA. IF ERROR-1 EXIT PARAGRAPH. IF ERROR-2 EXIT PARAGRAPH. * ... main processing ...
Multiple conditions can trigger early exit.
1. What is the primary purpose of the EXIT PARAGRAPH statement in COBOL?
2. Is EXIT PARAGRAPH required at the end of every paragraph?
3. What happens if you use EXIT PARAGRAPH in a section?
4. What is the difference between EXIT PARAGRAPH and EXIT SECTION?
5. When should you use EXIT PARAGRAPH?