PERFORM paragraph vs. PERFORM paragraph1 THRU paragraph2
PERFORM paragraph
Transfer control to paragraph, and transfer back when paragraph has finished to instruction immediately after the PERFORM instruction.
IDENTIFICATION DIVISION. PROGRAM-ID. MAINPROG. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. DISPLAY "MAIN ENTRY". PERFORM PARAGRAPH1. *> transfer control to PARAGRAPH1 DISPLAY "MAIN EXIT". STOP RUN. PARAGRAPH1. DISPLAY 'IN PARAGRAPH1'. *> transfer back here PARAGRAPH2. DISPLAY 'IN PARAGRAPH2'. PARAGRAPH3. DISPLAY 'IN PARAGRAPH3'. PARAGRAPH4. DISPLAY 'IN PARAGRAPH4'.
The execution result:
MAIN ENTRY IN PARAGRAPH1 MAIN EXIT
PERFORM paragraph1 THRU paragraph2
Transfer control to paragraphs from paragraph1 and paragraph2(paragraph itself is included); and transfer control back after parapraph2 has finished.
IDENTIFICATION DIVISION. PROGRAM-ID. MAINPROG. ENVIRONMENT DIVISION. DATA DIVISION. PROCEDURE DIVISION. DISPLAY "MAIN ENTRY". PERFORM PARAGRAPH1 THRU PARAGRAPH3. DISPLAY "MAIN EXIT". STOP RUN. PARAGRAPH1. DISPLAY 'IN PARAGRAPH1'. PARAGRAPH2. DISPLAY 'IN PARAGRAPH2'. PARAGRAPH3. DISPLAY 'IN PARAGRAPH3'. PARAGRAPH4. DISPLAY 'IN PARAGRAPH4'.
The execution result:
MAIN ENTRY IN PARAGRAPH1 IN PARAGRAPH2 IN PARAGRAPH3 MAIN EXIT
(PARAGRAPH3 is executed, but PARAGRAPH4 is not)
Notice: if there is control transferring instruction between paragraph1 and paragraph2, that transfer control to other paragraph, control will not back to PERFORM instruction.
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINPROG.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
DISPLAY "MAIN ENTRY".
PERFORM PARAGRAPH1 THRU PARAGRAPH3.
DISPLAY "MAIN EXIT".
STOP RUN.
PARAGRAPH1.
DISPLAY 'IN PARAGRAPH1'.
PARAGRAPH2.
DISPLAY 'IN PARAGRAPH2'.
GO TO PARAGRAPHX. *> transfer control to PARAGRAPHX
PARAGRAPH3.
DISPLAY 'IN PARAGRAPH3'.
PARAGRAPH4.
DISPLAY 'IN PARAGRAPH4'.
PARAGRAPHX.
DISPLAY 'IN PARAGRAPHX'. *> control stop here, and will not be back
The execute result:
MAIN ENTRY
IN PARAGRAPH1
IN PARAGRAPH2
IN PARAGRAPHX
Once control come to PARAGRAPHX, it will not back to PARAGRAPH3 again, and finally it will not back to the instruction immediately after PERFORM instruction.
PERFORM..THRU is dangerous
The PERFORM..THRU is dangers, but it's useful when dealing with error, we can stop executing a paragraph if an error is detected.
PROCEDURE DIVISION PERFORM PARAGRAPHX THRU E-PARAGRAPHX. STOP RUN. PARAGRAPHX. Statements IF ErrorFound GO TO E-PARAGRAPHX END-IF Statements E-PARAGRAPHX. EXIT.
(The EXIT statement in the E-PARAGRAPHX paragraph is a dummy statement. It has absolutely no effect on the flow of control. It is in the paragraph merely to conform to the rule that every paragraph must contain at least one sentence and in fact it must be the only sentence in the paragraph. It may be regarded as a comment.)
Usually, this should be the only case of a PERFORM..THRU and a GO..TO statement should be used. No other use of the PERFORM..THRU and GO...TO is acceptable.