Enums in COBOL

Our enum type ServerState has an underlying integer type. The possible values for ServerState are defined as constants.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. EnumeratedTypesDemo.
       DATA DIVISION.
       WORKING-STORAGE SECTION.

      * Define enum-like constants
       77  ServerState       PIC 9(02).
       77  StateIdle         VALUE 0.
       77  StateConnected    VALUE 1.
       77  StateError        VALUE 2.
       77  StateRetrying     VALUE 3.

      * Define a table to store state names
       01  StateNameTable.
           05 StateNameOccurs OCCURS 4 TIMES.
               10  StateNameValue       PIC X(10).
       PROCEDURE DIVISION.

      * Initialize table with state names
           PERFORM VARYING I FROM 1 BY 1 UNTIL I > 4
               EVALUATE I
                   WHEN 1 MOVE "idle" TO StateNameValue (I)
                   WHEN 2 MOVE "connected" TO StateNameValue (I)
                   WHEN 3 MOVE "error" TO StateNameValue (I)
                   WHEN 4 MOVE "retrying" TO StateNameValue (I)
               END-EVALUATE
           END-PERFORM.

      * Demonstrate state transitions
           CALL 'TRANSITION' USING StateIdle
                                   GIVING NewState.

           DISPLAY "Transitioned to state: " StateNameValue (NewState + 1).

           CALL 'TRANSITION' USING NewState
                                   GIVING NewState2.

           DISPLAY "Transitioned to state: " StateNameValue (NewState2 + 1).

           STOP RUN.

      * Transition procedure
       TRANSITION SECTION.
       TRANSITION-PARA.
           EVALUATE TRUE
               WHEN ServerState = StateIdle
                   MOVE StateConnected TO ServerState
               WHEN ServerState = StateConnected
                   MOVE StateIdle TO ServerState
               WHEN ServerState = StateRetrying
                   MOVE StateIdle TO ServerState
               WHEN ServerState = StateError
                   MOVE StateError TO ServerState
               WHEN OTHER
                   DISPLAY "Unknown state"
                   STOP RUN
           END-EVALUATE.
           EXIT PROGRAM.

Define the appropriate transition mechanism:

       TRANSITION PROCEDURE DIVISION USING State.
       TRANSITION-PARA.
           PERFORM EVALUATE TRANSITION-LOGIC  State.

           RETURNING ServerState.
       TRANSITION-LOGIC PARA.

           EVALUATE State
               WHEN StateIdle
                   MOVE StateConnected TO ServerState
               WHEN StateConnected
                   MOVE StateIdle TO ServerState
               WHEN StateRetrying
                   MOVE StateIdle TO ServerState
               WHEN StateError
                   MOVE StateError TO ServerState
               WHEN OTHER
                   DISPLAY "Unknown state"
                   STOP RUN
           END-EVALUATE.
           EXIT.
      ```

Running the COBOL program involves compiling it and then executing the binary.

```sh
$ cobc -x -free enumerated_types_demo.cbl
$ ./enumerated_types_demo
Transitioned to state: connected
Transitioned to state: idle

In this example, we defined a state machine using COBOL, initialized the state name table, performed state transitions, and displayed the transitions. Now that we can run and build basic COBOL programs, let’s learn more about the language.