cobol程序中的无限循环

时间:2015-01-20 12:06:47

标签: infinite-loop cobol

我是旧COBOL的新手。我正在尝试在线编译器的一个例子。但是,我正在使用的代码正在进入无限循环。代码是:

IDENTIFICATION DIVISION.
PROGRAM-ID.  Conditions.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  Char               PIC X.
    88 Vowel           VALUE "a", "e", "i", "o", "u".
    88 Consonant       VALUE "b", "c", "d", "f", "g", "h"
                             "j" THRU "n", "p" THRU "t", "v" THRU "z".
    88 Digit           VALUE "0" THRU "9".
    88 ValidCharacter  VALUE "a" THRU "z", "0" THRU "9".

PROCEDURE DIVISION.
Begin.
    DISPLAY "Enter lower case character or digit. No data ends.".
    ACCEPT Char.
    PERFORM UNTIL NOT ValidCharacter
        EVALUATE TRUE
           WHEN Vowel DISPLAY "The letter " Char " is a vowel."
           WHEN Consonant DISPLAY "The letter " Char " is a consonant."
           WHEN Digit DISPLAY Char " is a digit."
           WHEN OTHER DISPLAY "problems found"
        END-EVALUATE
    END-PERFORM.
    STOP RUN.

我理解(只是一个粗略的想法)是 PERFORM UNTIL 就像 EVALUATE 就像 SWITCH WHEN C 中的 CASE 类似。那么,输入有效字符时不应该循环中断吗?

2 个答案:

答案 0 :(得分:6)

您不会在循环中更改任何内容。循环中唯一的代码是EVALUATE(不更改数据)和四个DISPLAY语句(不更改任何数据)。

在不改变任何东西的情况下循环是一个无限循环。

您需要在ACCEPT之后添加第二个END-EVALUATE语句。

如果你用C编写了等价物,那么你也有一个无限循环。

答案 1 :(得分:1)

正如Bill Woodger所说,你没有改变char的值,所以它将永远循环。

此外,如果你纠正了这个问题,程序仍然无法正常工作:你有一些奇怪的逻辑:

  • PERFORM,而Char上有一个有效字符。
  • 但如果角色不是元音,辅音或数字,它将逃脱EVALUATE

我最好的猜测是你尝试它的方式:

IDENTIFICATION DIVISION.
PROGRAM-ID.  Conditions.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  Char               PIC X.
    88 Vowel           VALUE "a", "e", "i", "o", "u".
    88 Consonant       VALUE "b", "c", "d", "f", "g", "h"
                             "j" THRU "n", "p" THRU "t", "v" THRU "z".
    88 Digit           VALUE "0" THRU "9".
    88 ValidCharacter  VALUE "a" THRU "z", "0" THRU "9".

PROCEDURE DIVISION.
Begin.
    DISPLAY "Enter lower case character or digit. No data ends.".
    MOVE "a" to Char
    PERFORM UNTIL NOT ValidCharacter
        ACCEPT Char
        EVALUATE TRUE
           WHEN Vowel DISPLAY "The letter " Char " is a vowel."
           WHEN Consonant DISPLAY "The letter " Char " is a consonant."
           WHEN Digit DISPLAY Char " is a digit."
        END-EVALUATE
    END-PERFORM.
    DISPLAY "Non-valid character!"
    STOP RUN.

的变化:

  • 我将ACCEPT放在PERFORM
  • 因为它及其声明方式,Char现在有一个无效字符(空格)。因此,我移动一个有效字符(“a”)以使用MOVE "a" to Char验证执行条件。
  • 我摆脱了WHEN OTHER行,因为如果它真的是OTHER,那就意味着它不是元音,辅音或数字,这正是PERFORM正在做的事情:等待Char中的字符无效。

我的输出:

Enter lower case character or digit. No data ends.
d
The letter d is a consonant.
e
The letter e is a vowel.
q
The letter q is a consonant.
a
The letter a is a vowel.
1
1 is a digit.
@
Non valid character!

COBOL STOP RUN at line 218 in program TEST.CBL