Cobol True False Condition Testing

 

I had to create a Soap web service program recently. I work in a shop that uses Cobol as the primary development language. My options were dictated by that requirement. Rather than write something from scratch, I rummaged around and found the skeleton of a program that was much like what I needed to create. A major lucky break for me.

In this program that I recycles I found a real gem for True/False condition testing.

The closest thing Cobol has to a boolean data type is the 88-level entry codes entered in the Date Division. These 88-level entries, known as condition-names, are Cobol True/False condition test shortcuts. I have always thought they were a little clunky and didn’t use them unless they seemed especially appropriate.

To review, if you code in the Data Division something like:

   05 Marital-Status     PIC X.
       88  Single            VALUE  “s”.

Then in the Procedure Division you can code something like:

       MOVE "s" TO Marital-Status
       IF Single
           Do something…

Instead of

       MOVE "s" TO Marital-Status
       IF Marital-Status = “s”
           Do something...

I have always thought the latter is more transparent and less complicated. You can see that the result of a condition-name is always a true/false value. Cobol doesn’t really have a boolean data type. So these condition-codes and also the True/False condition test in the Evaluate statement is all you get.

What I found in this skeleton program that I started out this article talking about was a use of 88-level entries that incorporated the SET statement to produce a cleaner True/False condition test. Here’s an example.

So, in the Data Division you start with something like:

   01  Pgm-State            PIC x.
       88  End-Pgm             VALUE 'Y' FALSE 'N'.

This establishes the condition-name End-Pgm with a value of “Y” and “N” representing False.

Then in your initialization code you will set the value of this condition to a known state of False.

    SET End-Pgm TO FALSE

Now, in the Procedure Division you can set and evaluate this condition-name as needed.

    READ FileName
    INVALID KEY
       SET End-Pgm TO TRUE…

Later in the program…

    IF End-Pgm GOBACK.

I guess what I liked about this approach is how the code is so explicit. Not only is the result of the evaluation always either true or false, but so are the underlying values, and you are explicitly setting the condition-name to either True or False. It’s so much clearer as you are auditing code and trying to figure out how things work.

Stumbling on to this bit of code was a real stroke of luck for me. I will use this method again when I find myself needing to track program states that can be either true or false. I hope you find it code sample useful too.