goback和GO TO段落

时间:2016-04-19 22:53:46

标签: cobol

有什么方法可以让我取消cobol中“GO TO”的效果 例如:

    PERFORM PROCEDURE1 THRU E--PROCEDURE1
    display "PERFORM     PROCEDURE2 THRU E--PROCEDURE2"
    PERFORM     PROCEDURE2 THRU E--PROCEDURE2
    GOBACK.

   PROCEDURE1 SECTION.
       display "BEGIN============PROC PROCEDURE1"
       PERFORM LECTURE THRU E--LECTURE
       PERFORM ENDD THRU E--ENDD
       display "  ENDD============PROC PROCEDURE1"
       CONTINUE.
       EXIT.
   E--PROCEDURE1.
       EXIT.


    LECTURE.
       display "I AM LABEL LECTURE"
       GO TO ENDD
       CONTINUE.
    E--LECTURE.
       EXIT.

    ENDD.
       DISPLAY "I AM LABEL ENDD"
       CONTINUE.
    E--ENDD.
       EXIT.

    PROCEDURE2 SECTION.
       display "BEGIN============PROC PROCEDURE2"
       DISPLAY "I AM LABEL PROCEDURE2"
       display "  ENDD============PROC PROCEDURE2"
       CONTINUE.
    E--PROCEDURE2.
       EXIT.

当执行此代码时,我将看不到显示“PERFORM PROCEDURE2 THRU E - PROCEDURE2”,因为当我转到段落ENDD时,控件被永久地转移到该段中,我认为有我无法用到goback到proc1的结尾?我错了吗?

@Bill

这是我试图翻译的程序:

GOTO5POW : PROC OPTIONS(MAIN);
DCL FILE001 FILE INPUT RECORD ENV(RECSIZE(15));
DCL CTR1  PIC'99' INIT('0');
DCL CARTE CHAR(15);
CALL PROCEDURE1;
PUT SKIP LIST("PERFORM     PROCEDURE2 THRU E--PROCEDURE2");
CALL PROCEDURE2;
PROCEDURE1: PROC;
      PUT SKIP LIST("BEGIN============PROC PROCEDURE1");
      ON ENDFILE(FILE001) GOTO ENDD;
      LECTURE:
      PUT SKIP LIST("I AM LABEL LECTURE");
      READ FILE(FILE001) INTO(CARTE);
      CTR1 = CTR1 + 1;
      PUT SKIP LIST("Record "||CTR1||"=/" || CARTE ||"/");
      IF CARTE="AAAAAAAAAAAAAAA" THEN GOTO ENDD;
      ENDD:
         PUT SKIP LIST("I AM LABEL ENDD");
      PUT SKIP LIST("  ENDD============PROC PROCEDURE1");
END PROCEDURE1;
PROCEDURE2 : PROC;
    PUT SKIP LIST("BEGIN============PROC PROCEDURE2");
    PUT SKIP LIST("I AM LABEL PROCEDURE2");

    PUT SKIP LIST("  ENDD============PROC PROCEDURE2");
END PROCEDURE2;
END GOTO5POW ;

输出结果为:

BEGIN============PROC PROCEDURE1
I AM LABEL LECTURE
Record 01=/AAAAAAAAAAAAAAA/
I AM LABEL ENDD
  ENDD============PROC PROCEDURE1
PERFORM     PROCEDURE2 THRU E--PROCEDURE2
BEGIN============PROC PROCEDURE2
I AM LABEL PROCEDURE2
  ENDD============PROC PROCEDURE2

但是对于cobol,这是生成的输出:

BEGIN============PROC PROCEDURE1
I AM LABEL LECTURE
Record 01=/AAAAAAAAAAAAAAA/
I AM LABEL ENDD
BEGIN============PROC PROCEDURE2
I AM LABEL PROCEDURE2
  ENDD============PROC PROCEDURE2

1 个答案:

答案 0 :(得分:2)

正如法案所说,不要直接使用。也可以使用程序或部分; 不是两者。 ON ENDFILE(FILE001)为文件结尾

设置异常处理程序

这大致是你的计划应该是:

            03                               pic x value 'N'.
               88 File001-EOF         value 'Y'.
               88 File001-has-data    value 'N'.

       PERFORM PROCEDURE1 
       display "PERFORM     PROCEDURE2 THRU E--PROCEDURE2"
       PERFORM PROCEDURE2 
       GOBACK.

       PROCEDURE1 SECTION.
          display "BEGIN============PROC PROCEDURE1"
          display "I AM LABEL LECTURE"
          Read File001
            at end set File001-EOF               to true
          end-read
          if File001-has-data
              compute CTR1 = CTR1 + 1;
          end-if

          DISPLAY "I AM LABEL ENDD"
          display "  ENDD============PROC PROCEDURE1"

          EXIT.
       E--PROCEDURE1.
          EXIT.


       PROCEDURE2 SECTION.
          display "BEGIN============PROC PROCEDURE2"
          DISPLAY "I AM LABEL PROCEDURE2"
          display "  ENDD============PROC PROCEDURE2"
          CONTINUE.
       E--PROCEDURE2.
          EXIT.

对于更熟悉java / c#的人

ON ENDFILE(FILE001) GOTO ENDD;

为End-Of-File条件设置异常或错误处理程序。在java中它就像:

try {
   .... 
} catch (endOfFile e) {
   Goto ENDD;
}