Cobol程序中的无限循环
Infinite loop in cobol program
我是旧 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 就像 while, EVALUATE 就像 SWITCH 而 WHEN 就像 CASE in C。那么,循环是否应该在输入有效字符时中断?
您不要在循环中更改任何内容。循环中唯一的代码是 EVALUATE
(不更改数据)和四个 DISPLAY
语句(不更改数据)。
循环而不改变任何东西是一个无限循环。
您需要在 END-EVALUATE
之后添加第二个 ACCEPT
语句。
如果您用 C 编写了等效代码,您也会遇到无限循环。
正如 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 现在有一个无效字符(空 space)。所以我移动了一个有效字符 ("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
我是旧 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 就像 while, EVALUATE 就像 SWITCH 而 WHEN 就像 CASE in C。那么,循环是否应该在输入有效字符时中断?
您不要在循环中更改任何内容。循环中唯一的代码是 EVALUATE
(不更改数据)和四个 DISPLAY
语句(不更改数据)。
循环而不改变任何东西是一个无限循环。
您需要在 END-EVALUATE
之后添加第二个 ACCEPT
语句。
如果您用 C 编写了等效代码,您也会遇到无限循环。
正如 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 现在有一个无效字符(空 space)。所以我移动了一个有效字符 ("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