Monday, 28 July 2014

COBOL Complex Conditions: AND, OR, NOT, and Parentheses

A COBOL IF statement can look correct and still send a record down the wrong path when AND, OR, and NOT are mixed without parentheses. Complex conditions are useful, but they need clear grouping so the business rule is readable in production code.

COBOL complex condition diagram showing simple conditions joined by AND OR NOT and a true path
Group the condition clearly.

What is a complex condition in COBOL?

A complex condition is a condition built by combining simple conditions with logical operators or by negating a condition. IBM lists AND, OR, and NOT as the logical operators used for this work.

Simple conditions include relation conditions, class tests such as IS NUMERIC, sign tests such as IS NEGATIVE, and condition-name tests such as 88-level names. A complex condition lets one IF test more than one fact.

Logical operators

Operator Meaning Example
AND Both conditions must be true. WS-AMT > ZERO AND WS-STATUS = 'A'
OR One or both conditions can be true. WS-TYPE = 'C' OR WS-TYPE = 'S'
NOT Reverses the truth value of a condition. NOT WS-AMT IS NUMERIC

Order of evaluation

IBM documents the default precedence as arithmetic operations first, then simple conditions, then NOT, then AND, then OR. Parentheses can change that order and make the intended rule obvious.

IF WS-STATUS = 'A'
   AND WS-BALANCE > ZERO
   OR WS-VIP-CUSTOMER = 'Y'
    PERFORM SEND-LETTER
END-IF

That condition may not mean what the reviewer expects. It is safer to group the rule.

IF (WS-STATUS = 'A' AND WS-BALANCE > ZERO)
   OR WS-VIP-CUSTOMER = 'Y'
    PERFORM SEND-LETTER
END-IF

Use parentheses for business rules

Parentheses are cheap. They prevent a maintainer from guessing whether the rule is driven by account status, balance, customer type, or all three.

IF WS-ORDER-AMOUNT > 1000
   AND (WS-CREDIT-STATUS = 'A'
        OR WS-MANAGER-APPROVED = 'Y')
    PERFORM RELEASE-ORDER
END-IF

Here the high order amount is always required. Either good credit status or manager approval can satisfy the second part of the rule.

Class and sign conditions

Class tests and sign tests make validation code easier to read. IBM documents class conditions such as IS NUMERIC and sign conditions such as IS POSITIVE, IS NEGATIVE, and IS ZERO.

IF WS-INPUT-AMOUNT IS NUMERIC
   AND WS-INPUT-AMOUNT IS POSITIVE
    PERFORM PROCESS-AMOUNT
ELSE
    PERFORM REJECT-RECORD
END-IF

Be careful with signed numeric fields and edited fields. A class test is not a substitute for understanding the field definition in the copybook.

Use 88-level names where they help

Condition-name entries can make a complex condition read like a business sentence. They are especially useful for status codes, record types, and yes/no flags.

01 WS-ACCOUNT-STATUS     PIC X.
   88 ACTIVE-ACCOUNT     VALUE 'A'.
   88 CLOSED-ACCOUNT     VALUE 'C'.

01 WS-ORDER-FLAG         PIC X.
   88 MANAGER-APPROVED   VALUE 'Y'.

IF ACTIVE-ACCOUNT
   AND (WS-BALANCE > ZERO OR MANAGER-APPROVED)
    PERFORM RELEASE-ORDER
END-IF

Abbreviated combined relation conditions

COBOL allows abbreviated combined relation conditions. IBM explains that the missing subject or relation operator can be taken from the last stated one. This can be compact, but it is also easy to misread.

IF WS-CODE = 'A' OR 'B' OR 'C'
    PERFORM VALID-CODE
END-IF

For beginner-friendly production code, the longer form is often clearer.

IF WS-CODE = 'A'
   OR WS-CODE = 'B'
   OR WS-CODE = 'C'
    PERFORM VALID-CODE
END-IF

Common mistakes

Mixing AND and OR without grouping

This is the most common readability problem. Add parentheses when both operators appear in the same condition.

Putting NOT in the wrong place

NOT A = B OR C is not the same as NOT (A = B OR A = C). Group negated conditions when the rule covers more than one comparison.

Writing a condition that hides the business term

If an IF spans many lines, consider 88-level condition names or a small validation paragraph with a clear name.

Review checklist

  • Use parentheses when AND and OR appear together.
  • Check the position of NOT.
  • Use 88-level names for status codes and flags.
  • Check class tests against the actual PIC and USAGE clauses.
  • Keep each line short enough to review in a normal editor.
  • Add test cases for each true and false branch.

Related Mainframe Forum guides

For nearby topics, read COBOL IF statement, COBOL EVALUATE statement, COBOL 88-level condition names, COBOL data types, COBOL CONTINUE and NEXT SENTENCE, and COBOL decision making.

External references

IBM documents complex conditions, combined conditions, and abbreviated combined relation conditions.

FAQ

What is a complex condition in COBOL?

A complex condition combines simple conditions with AND, OR, or NOT, or negates a condition.

Which operator is evaluated first in COBOL?

After arithmetic and simple conditions, COBOL evaluates NOT, then AND, then OR, unless parentheses change the order.

Should I always use parentheses?

Use parentheses when the condition mixes AND and OR, when NOT applies to a group, or when a reviewer could read the rule two ways.

Are 88-level names useful in complex conditions?

Yes. 88-level names make status-code and flag checks easier to read and reduce repeated literal comparisons.

No comments:

Post a Comment

New In-feed ads