有没有办法让执行不结束(比如无限循环)?

Is there a way I could make a perform not end (like an infinite while loop)?

有办法吗?我希望 perform 仅在执行 exit 语句时退出。如果不是,我希望它继续循环。

您可以使用具有给定条件的 "perform until" 退出

在 Cobol 中有三种基本方法可以做到这一点:

  • 循环展开 - 你重复一些代码所以 P
  • 退出执行
  • 转到

  • 使用 Next Sentence 代替 Go To 可能会奏效,但我不建议这样做

循环展开

下面Part-a是测试前要执行的代码

Perform Part-a
Perform until condition
   .....
   Perform Part-a
end-perform

退出执行

    Perform until end-of-the-world
       .....
       if condition
          Exit Perform
       end-if
       ...
    end-perform

Exit-Label.
    continue.

转到

    Perform until end-of-the-world
       .....
       if condition
          Go To Exit-Label
       end-if
       ...
    end-perform

Exit-Label.
    continue.

这是 Gary Cutler 的 GnuCOBOL 2.0 编程指南中 PERFORM 的语法图:

根据下面的描述,第 4 点:

The UNTIL EXIT option will repeatedly execute the code within the scope of the PERFORM with no conditions defined on the PERFORM statement itself for termination of the repetition. It will be up to the programmer to include an EXIT PERFORM within the scope of the PERFORM that will break out of the loop.

而且,最初没有意识到它在一个单独的点,第 5 点:

The FOREVER option has the same effect as UNTIL EXIT.

待进一步说明,这可能是也可能不是您想要的。

掌握相关的编程指南,并使用它。读。实验。重复直到理解或没有进展。如果没有进展,请询问。同事们,GnuCOBOL 讨论区,或者这里。

我喜欢使用 "PERFORM FOREVER",因为它清楚地将代码标识为无限循环。 "PERFORM UNTIL EXIT" 也有效。下面是一些使用无限循环和 "EXIT PERFORM" 语句打印数字 1 到 10 的示例代码。此代码适用于 GNUCobol 2.0。

   IDENTIFICATION DIVISION.
   PROGRAM-ID. INFINITE-LOOP.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.

   DATA DIVISION.
   FILE SECTION.

   WORKING-STORAGE SECTION.
   01  COUNTER                          PIC 99 VALUE ZERO.

   PROCEDURE DIVISION.

  * USE EITHER OF THE TWO FOLLOWING LINES
  * WHICHEVER YOU FIND MORE MEANINGFUL
  *    PERFORM UNTIL EXIT
       PERFORM FOREVER
           ADD 1 TO COUNTER
           DISPLAY COUNTER
           IF COUNTER > 9
               EXIT PERFORM
           END-IF
       END-PERFORM
       STOP RUN
       .