COBOL PERFORM verb



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.










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值