COBOL PERFORM verb

来源:互联网 发布:信息工程研究所 知乎 编辑:程序博客网 时间:2024/06/01 08:20



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 ENTRYIN PARAGRAPH1MAIN EXIT




PERFORM paragraph1 THRU paragraph2

Transfer control to paragraphs from paragraph1 and paragraph2(paragraph itself is included); and transfer control back afterparapraph2 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 ENTRYIN PARAGRAPH1IN PARAGRAPH2IN PARAGRAPH3MAIN EXIT

(PARAGRAPH3 is executed, but PARAGRAPH4 is not)



Notice: if there is control transferring instruction between 
paragraph1and 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 ENTRYIN PARAGRAPH1IN PARAGRAPH2IN 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   StatementsE-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 thatevery 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.










0 0