GO TO DEPENDING

How it Works

GO TO DEPENDING is a way of selecting a destination according to the current value of a variable. For example:
     GO TO 3100-PROCESS-HEADER-REC
           3200-PROCESS-ORDER-REC
           3300-PROCESS-WTN-REC
           3400-PROCESS-TRAILER-REC
        DEPENDING ON RECTYPE-FLAG.
...where RECTYPE-FLAG is an integer variable. If RECTYPE equals 1, we GO TO the first destination. If it equals 2, we GO TO the second destination, and so forth. If RECTYPE-FLAG is greater than the number of destinations listed, the GO TO has no effect, and control falls into the next statement.

The compiler can implement GO TO DEPENDING by storing each destination in an array, using the integer variable as a subscript. At run time the processor can find the destination directly without having to pick its way through a long list of alternatives.

Transformation

Transform a GO TO DEPENDING into the equivalent case structure:
     EVALUATE RECTYPE-FLAG
        WHEN 1
           GO TO 3100-PROCESS-HEADER-REC
        WHEN 2
           GO TO 3200-PROCESS-ORDER-REC
        WHEN 3
           GO TO 3300-PROCESS-WTN-REC
        WHEN 4
           GO TO 3400-PROCESS-TRAILER-REC
        WHEN OTHER
           CONTINUE
     END-EVALUATE.
This version is more verbose, and it has more GO TOs, but it will be easier to work with later. Since you have uncoupled the GO TOs from each other, you can apply later transformations to them separately.
[style forum]COBOL Style Forum [spaghetti]Spaghetti code [stage]Next stage
[home]COBOL Home