Showing posts with label COBOL condition names. Show all posts
Showing posts with label COBOL condition names. Show all posts

Monday, 28 July 2014

COBOL Level 88 Condition Names: VALUE, SET, and Examples

A program reads STATUS-CODE as A, S, or D. Repeating those literals in every IF makes the business meaning easy to miss. COBOL level 88 condition names let the program ask whether RECORD-ACTIVE is true and set that named condition directly, while the one-byte STATUS-CODE remains the field that stores the value.

COBOL level 88 condition names mapping active, suspended, and deleted values to STATUS-CODE
Level-88 names test or set meaningful values in an existing conditional variable without allocating storage.

What a COBOL level 88 condition name is

Level 88 identifies a condition-name associated with a particular value, set of values, or range of values in another data item. That preceding data item is the conditional variable. When the program references the condition-name, COBOL compares the current contents of the conditional variable with the values in the level-88 VALUE clause.

A condition-name does not reserve bytes and is not a Boolean data item. It has no PICTURE clause. The conditional variable owns the storage and data description; the 88 entry gives one or more of its values a readable name.

The compiler evaluates that named condition each time it is referenced. No separate flag is synchronized behind the scenes, so direct changes to the conditional variable take effect immediately.

Level 88 syntax and placement

05  conditional-variable  PIC X.
    88  condition-name
        VALUE literal-1 [literal-2 ...]
              [THROUGH literal-3]
        [WHEN SET TO FALSE IS literal-4].

The VALUE clause is required and is the only clause in a condition-name entry. Level-88 entries for a variable must immediately follow that variable or another 88 entry associated with it. The condition values must be compatible with the conditional variable's category and PICTURE. See the COBOL Data Division guide for surrounding record structure.

Conditional variable versus condition-name

PropertyConditional variableLevel-88 condition-name
ExampleSTATUS-CODERECORD-ACTIVE
StorageOwns the byte or bytesAllocates no storage
Data descriptionHas PICTURE and optional USAGEHas a VALUE clause only
Procedural useCan be moved, compared, or displayedCan be tested as a condition or used by SET
TruthContains the current valueTrue when that value matches its VALUE set

Simple switch example

A two-state switch uses one underlying field with meaningful true and false condition names:

01  PROCESS-SWITCHES.
    05  EOF-SWITCH        PIC X VALUE 'N'.
        88  END-OF-FILE   VALUE 'Y'.
        88  MORE-RECORDS  VALUE 'N'.

IF MORE-RECORDS
    CONTINUE
END-IF

END-OF-FILE becomes true when EOF-SWITCH contains Y; MORE-RECORDS is true when it contains N. Both names refer to the same one-byte switch.

SET condition-name TO TRUE

SET TO TRUE moves the value associated with a condition-name into its conditional variable. It expresses intent more clearly than repeating a literal:

SET END-OF-FILE TO TRUE

*> Same resulting value:
MOVE 'Y' TO EOF-SWITCH

When a condition-name lists multiple values or ranges, IBM Enterprise COBOL assigns the first value when SET TO TRUE executes. Choose that first value deliberately instead of assuming which member of the set will be stored. The broader COBOL SET statement guide covers its other formats.

SET TO FALSE with WHEN SET TO FALSE

Enterprise COBOL allows a level-88 VALUE clause to identify the exact value used by SET TO FALSE:

05  EOF-SWITCH        PIC X VALUE 'N'.
    88  END-OF-FILE   VALUE 'Y'
                      WHEN SET TO FALSE IS 'N'.

SET END-OF-FILE TO TRUE
SET END-OF-FILE TO FALSE

The first SET stores Y; the second stores N. The FALSE phrase does not say that only N makes the condition false. Every value outside the true VALUE set tests false; the phrase merely selects the value that SET TO FALSE will store.

Multiple values for one condition

One condition-name can represent several noncontiguous values:

05  STATUS-CODE           PIC X.
    88  RECORD-OPEN       VALUE 'A' 'S' 'H'.
    88  RECORD-CLOSED     VALUE 'D' 'X'.

RECORD-OPEN is true for A, S, or H. SET RECORD-OPEN TO TRUE stores the first listed value, A. Separate values with spaces, commas, or semicolons according to normal COBOL separator rules.

VALUE ranges with THROUGH or THRU

THROUGH and THRU are equivalent. They define an inclusive range, and the starting literal must be less than the ending literal:

05  EXAM-SCORE             PIC 9(3).
    88  PASSING-SCORE     VALUE 060 THRU 100.
    88  FAILING-SCORE     VALUE 000 THRU 059.

If EXAM-SCORE contains 060 or 100, PASSING-SCORE is true because both endpoints are included. Keep the literal class compatible with the conditional variable. The COBOL PICTURE and data-type guide explains numeric and alphanumeric definitions.

Using level 88 with IF

A condition-name can replace a repeated relation condition:

IF RECORD-CLOSED
    DISPLAY 'RECORD CANNOT BE UPDATED'
ELSE
    PERFORM UPDATE-RECORD
END-IF

This test uses the VALUE definitions beside STATUS-CODE, so a future value change is made in the Data Division rather than in every IF. The COBOL IF statement guide covers nested conditions and explicit scope terminators.

Using condition names with EVALUATE TRUE

EVALUATE TRUE
    WHEN RECORD-ACTIVE
        PERFORM PROCESS-ACTIVE
    WHEN RECORD-SUSPENDED
        PERFORM PROCESS-SUSPENDED
    WHEN RECORD-DELETED
        PERFORM PROCESS-DELETED
    WHEN OTHER
        PERFORM REPORT-BAD-STATUS
END-EVALUATE

EVALUATE TRUE selects the first true WHEN condition. Define value sets so that overlapping condition-names cannot silently choose an earlier branch unless that precedence is intentional. See the COBOL EVALUATE guide for subjects, objects, and WHEN OTHER.

EOF loop example with PERFORM UNTIL

MOVE 'N' TO EOF-SWITCH

PERFORM UNTIL END-OF-FILE
    READ CUSTOMER-FILE
        AT END
            SET END-OF-FILE TO TRUE
        NOT AT END
            PERFORM PROCESS-CUSTOMER
    END-READ
END-PERFORM

Initialize the switch before the loop. An uninitialized value might make both END-OF-FILE and MORE-RECORDS false, leaving the loop logic dependent on data left in storage. Review PERFORM UNTIL and TEST BEFORE for loop timing.

File-status condition names

A two-byte FILE STATUS item is a useful conditional variable because the program often cares about named outcomes:

SELECT CUSTOMER-FILE ASSIGN TO CUSTIN
    ORGANIZATION IS SEQUENTIAL
    FILE STATUS IS CUSTOMER-FILE-STATUS.

01  CUSTOMER-FILE-STATUS  PIC XX.
    88  READ-SUCCESSFUL   VALUE '00'.
    88  END-OF-FILE       VALUE '10'.

Do not hide every code under one broad name; distinguish normal end-of-file from errors that need logging or recovery. The COBOL file-status code guide provides the I/O context.

Condition names in OCCURS tables

If the conditional variable is in a table, references to its condition-name require the same subscript or index:

01  ITEM-TABLE.
    05  ITEM-ENTRY OCCURS 10 TIMES
                   INDEXED BY ITEM-IDX.
        10  ITEM-STATUS    PIC X.
            88  ITEM-READY VALUE 'R'.

IF ITEM-READY (ITEM-IDX)
    PERFORM PROCESS-ITEM
END-IF

Testing ITEM-READY without identifying an occurrence is ambiguous. The subscript or index selects the ITEM-STATUS byte whose value is tested.

Qualification and naming

Condition-names must be unique where referenced or made unique through qualification and subscripting. IBM permits the associated conditional variable as the first qualifier, followed by its normal hierarchy. Unique business names are usually easier to review than repeated names that depend on long qualification.

Name the true business condition: PAYMENT-OVERDUE, READ-SUCCESSFUL, or CUSTOMER-BLOCKED. Avoid names such as FLAG-ON when the reader still has to find the field to learn what the flag controls.

Common level-88 mistakes

  • Expecting separate storage: moving to the conditional variable immediately changes which 88 names test true.
  • Adding a PICTURE clause: a level-88 entry requires VALUE as its only clause.
  • Wrong placement: the condition-name does not immediately follow its conditional variable or another associated 88 entry.
  • Incompatible literal: an alphanumeric value is paired with a numeric conditional variable or exceeds its defined size.
  • Overlapping conditions: two 88 names are true for the same value and EVALUATE selects the first matching WHEN.
  • Uninitialized switch: neither the positive nor negative condition is true when processing begins.
  • Assuming SET TO TRUE chooses any valid value: it stores the first value in the condition-name's VALUE set.
  • Missing table subscript: a condition-name associated with an OCCURS item is referenced without the required occurrence.

Production review checklist

  1. Define and initialize the conditional variable with the right PICTURE and USAGE.
  2. Place every associated level-88 entry immediately after that variable.
  3. Check that single values and range endpoints fit the variable.
  4. Confirm that ranges are inclusive, ordered, and do not overlap unintentionally.
  5. Verify the first value selected by each SET TO TRUE statement.
  6. Define WHEN SET TO FALSE before using SET TO FALSE.
  7. Use the same subscript or index required by an OCCURS conditional variable.
  8. Compile with the target Enterprise COBOL release and retain the full diagnostic.

IBM Enterprise COBOL references

COBOL level 88 FAQ

Does a COBOL level 88 item occupy storage?

No. The conditional variable occupies storage. Its level-88 entries associate readable condition-names with values that the variable can contain.

Can one level-88 condition-name have several values?

Yes. Its VALUE clause can list separate values and inclusive THROUGH or THRU ranges. The condition is true when the conditional variable matches any value in that set.

What value does SET condition-name TO TRUE store?

It stores the first value associated with the condition-name. For a multiple-value condition, arrange the list so that the intended set value appears first.

Can level 88 be used with IF, EVALUATE, and PERFORM UNTIL?

Yes. A condition-name is a conditional expression and can be tested anywhere that form of condition is allowed, including IF, EVALUATE, PERFORM, and SEARCH.

Final check: inspect the conditional variable's actual bytes first; every level-88 result follows from that stored value.

New In-feed ads