Coding man
Coding man

Reputation: 967

Infinite loop in cobol program

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

Answers (2)

Molusco
Molusco

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:

  • It will PERFORM while Char has a valid character on it.
  • But it will escape the 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:

  • I put the ACCEPT inside the PERFORM.
  • Because that, and the way it was declared, Char now has an invalid character (empty space). So i move a valid character ("a") to validate the perform criteria with MOVE "a" to Char.
  • I got rid of the 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

Bill Woodger
Bill Woodger

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

Related Questions