ILE COBOL Language Reference

Complex Conditions

A complex condition is formed by combining simple conditions, combined conditions, and/or complex conditions with logical operators, or negating these conditions with logical negation.

Each logical operator must be preceded and followed by a space. The following chart shows the logical operators and their meanings.


Table 24. Logical Operators and Their Meanings

Logical Operator   Name   Meaning
  AND Logical conjunction The truth value is true when both conditions are true.
  OR Logical inclusive OR The truth value is true when either or both conditions are true.
  NOT Logical negation Reversal of truth value (the truth value is true if the condition is false).

Unless modified by parentheses, the following precedence rules (from highest to lowest) apply:

  1. Arithmetic operations
  2. Simple conditions
  3. NOT
  4. AND
  5. OR

The truth value of a complex condition (whether parenthesized or not) is the truth value that results from the interaction of all the stated logical operators on either of the following:

A complex condition can be either of the following:

Negated Simple Conditions

A simple condition is negated through the use of the logical operator NOT. The negated simple condition gives the opposite truth value of the simple condition.

Negated Simple Condition - Format
 
>>-NOT--simple-condition---------------------------------------><
 
 

Combined Conditions

Two or more conditions can be logically connected to form a combined condition.

Combined Conditions - Format
 
                .----------------------.
                V                      |
>>-condition-1----+-AND-+--condition-2-+-----------------------><
                  '-OR--'
 
 

The condition to be combined may be any of the following:


Table 25. Combined Conditions--Permissible Element Sequences

Combined condition element Leftmost When not leftmost, can be immediately preceded by: Rightmost When not rightmost, can be immediately followed by:
simple-condition Yes OR
NOT
AND
(
Yes OR
AND
)
OR
AND
No simple-condition
)
No simple-condition
NOT
(
NOT Yes OR
AND
(
No simple-condition
(
( Yes OR
NOT
AND
(
No simple-condition
NOT
(
) No simple-condition
)
Yes OR
AND
)

Parentheses are never needed when either ANDs or ORs (but not both) are used exclusively in one combined condition. However, parentheses may be needed to modify the implicit precedence rules to maintain the correct logical relation of operators and operands.

There must be a one-to-one correspondence between left and right parentheses, with each left parenthesis to the left of its corresponding right parenthesis.

The following table illustrates the relationships between logical

operators and conditions C1 and C2.

Table 26. Logical Operators and Evaluation Results of Combined Conditions

Value for C1 Value for C2 C1 AND C2 C1 OR C2 NOT (C1 AND C2) NOT C1 AND C2 NOT (C1 OR C2) NOT C1 OR C2
True True True True False False False True
False True False True True True False True
True False False True True False False False
False False False False True False True True

Related Information:

Evaluating Conditional Expressions

If parentheses are used, logical evaluation of combined conditions proceeds in the following order:

  1. Conditions within parentheses are evaluated first.
  2. Within nested parentheses, evaluation proceeds from the least inclusive condition to the most inclusive condition.

If parentheses are not used (or are not at the same level of inclusiveness), the combined condition is evaluated in the following order:

  1. Arithmetic expressions.
  2. Simple-conditions in the following order:
    1. Relation
    2. Class
    3. Condition-name
    4. Switch-status
    5. Sign.
  3. Negated simple-conditions in the same order as item 2.
  4. Combined conditions, in the following order:
    1. AND
    2. OR.
  5. Negated combined conditions in the following order:
    1. AND
    2. OR.
  6. Consecutive operands at the same evaluation-order level are evaluated from left to right. However, the truth value of a combined condition can sometimes be determined without evaluating the truth value of all the component conditions.

The component conditions of a combined condition are evaluated from left to right. If the truth value of one condition is not affected by the evaluation of further elements of the combined condition, these elements are not evaluated. However, the truth value of the condition will always be the same (as if the condition had been evaluated in full), as described earlier in this paragraph.

Values are established for arithmetic expressions and functions if and when the conditions containing them are evaluated. Similarly, negated conditions are evaluated if and when it is necessary to evaluate the complex condition that they represent.

For example:
NOT A IS GREATER THAN B OR A + B IS EQUAL
TO C AND D IS POSITIVE

is evaluated as if parenthesized as follows:
(NOT (A IS GREATER THAN B)) OR (((A+B) IS EQUAL
TO C) AND (D IS POSITIVE))

The order of evaluation in this example is as follows:

  1. (NOT (A IS GREATER THAN B)) is evaluated. If true, the rest of the condition is not evaluated, as the expression is true.
  2. (A+B) is evaluated, giving some intermediate result, x.
  3. (x IS EQUAL TO C) is evaluated. If false, the rest of the condition is not evaluated, as the expression is false.
  4. (D IS POSITIVE) is evaluated, giving the final truth value of the expression.

Abbreviated Combined Relation Conditions

When relation-conditions are written consecutively without intervening parentheses,

any relation-condition after the first can be abbreviated in one of two ways:

Abbreviated Combined Relation Conditions - Format
 
>>-relation-condition------------------------------------------->
 
   .-------------------------------------------------------------.
   V                                                             |
>----+-AND-+-+----+-+----------+-+----------------------+-object-+-><
     '-OR--' '-IS-' |     (1)  | +-GREATER-+------+-----+
                    '-NOT------' |         '-THAN-'     |
                                 +->--------------------+
                                 +-LESS--+------+-------+
                                 |       '-THAN-'       |
                                 +-<--------------------+
                                 +-EQUAL--+----+--------+
                                 |        '-TO-'        |
                                 +-=--------------------+
                                 +-| greater or equal |-+
                                 +->=-------------------+
                                 +-| less or equal |----+
                                 '-<=-------------------'
 
greater or equal:
 
|--GREATER--+------+--OR EQUAL--+----+--------------------------|
            '-THAN-'            '-TO-'
 
less or equal:
 
|--LESS--+------+--OR EQUAL--+----+-----------------------------|
         '-THAN-'            '-TO-'
 
 

Notes:

  1. NOT GREATER THAN OR EQUAL TO, NOT >=, NOT LESS THAN OR EQUAL TO, and NOT <=, are IBM Extensions.

An object is any data item or expression that can be compared to the subject of the preceding relation condition.

In any consecutive sequence of relation-conditions, both forms of abbreviation can be specified. The abbreviated condition is evaluated as if:

  1. The last stated subject is the missing subject.
  2. The last stated relational operator is the missing relational operator.

The resulting combined condition must comply with the rules for element sequence in combined conditions, as shown in Table 27.

The word NOT is considered part of the relational operator in the forms NOT GREATER THAN, NOT >, NOT LESS THAN, NOT <, NOT EQUAL TO, and NOT =.

NOT in any other position is considered a logical operator (and thus results in a negated relation-condition).

The following examples illustrate abbreviated combined relation conditions, with and without parentheses, and their unabbreviated equivalents.

Table 27. Abbreviated Combined Relation Conditions

Abbreviated Combined Relation Condition Equivalent
A = B AND NOT < C OR D
((A = B) AND (A NOT < C))  OR (A NOT < D)
A NOT > B OR C (A NOT > B)  OR (A NOT > C)
NOT A = B OR C (NOT (A = B)) OR (A = C)
NOT (A = B OR < C) NOT ((A = B) OR (A < C))
NOT (A NOT = B AND C AND NOT D) NOT ((((A NOT = B) AND (A NOT = C))  AND (NOT (A NOT = D))))


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]