Reputation: 967
I am new to old COBOL. I am trying an example on online compiler. But, the code I am using is entering an infinite loop. The code is :
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.
What I understand (only a rough idea) is that PERFORM UNTIL is like while, EVALUATE is like SWITCH and WHEN is like CASE in C. So, shouldn't the loop break on entering a valid character?
Upvotes: 1
Views: 3151
Reputation: 69
As Bill Woodger said, you didn't change the value of char, so it will keep looping forever.
Moreso, if you correct that issue, the program will still not work properly: you have some strange logic on it:
PERFORM
while Char has a valid character on it.EVALUATE
if the character is not a Vowel, Consonant or Digit.My best guess is this is how you attempt it to work:
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.
Changes:
ACCEPT
inside the PERFORM
.MOVE "a" to Char
.WHEN OTHER
line, because if it really is OTHER
, it means it is not a Vowel, Consonant or Digit, and that's exactly what the PERFORM
is doing: waiting until there's an invalid character in Char.My output:
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
Upvotes: 1
Reputation: 13076
You do not change anything within the loop. The only code you have within the loop is an EVALUATE
(changes no data) and four DISPLAY
statements (changes no data).
Looping whilst changing nothing is an infinite loop.
You need to include a second ACCEPT
statement after the END-EVALUATE
.
If you had coded the equivalent in C, you'd have got an infinite loop as well.
Upvotes: 6