Combined conditions

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

Format

Read syntax diagramSkip visual syntax diagramcondition-1ANDORcondition-2

The condition to be combined can be any of the following ones:

  • A simple-condition
  • A negated simple-condition
  • A combined condition
  • A negated combined condition (that is, the NOT logical operator followed by a combined condition enclosed in parentheses)
  • A combination of the preceding conditions that is specified according to the rules in the following table

Table 1. Combined conditionspermissible element sequences
Combined condition element Left most When not leftmost, can be immediately preceded by: Right most 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 might 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 2. 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

Order of evaluation of conditions

Parentheses, both explicit and implicit, define the level of inclusiveness within a complex condition. Two or more conditions connected by only the logical operators AND or OR at the same level of inclusiveness establish a hierarchical level within a complex condition. Therefore an entire complex condition is a nested structure of hierarchical levels, with the entire complex condition being the most inclusive hierarchical level.

Within this context, the evaluation of the conditions within an entire complex condition begins at the left of the condition. The constituent connected conditions within a hierarchical level are evaluated in order from left to right, and evaluation of that hierarchical level terminates as soon as a truth value for it is determined, regardless of whether all the constituent connected conditions within that hierarchical level have been evaluated.

Values are established for arithmetic expressions and functions if and when the conditions that contain 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))

Order of evaluation:

  1. (NOT (A IS GREATER THAN B)) is evaluated, giving some intermediate truth value, t1. If t1 is true, the combined condition is true, and no further evaluation takes place. If t1 is false, evaluation continues as follows.
  2. (A + B) is evaluated, giving some intermediate result, x.
  3. (x IS EQUAL TO C) is evaluated, giving some intermediate truth value, t2. If t2 is false, the combined condition is false, and no further evaluation takes place. If t2 is true, the evaluation continues as follows.
  4. (D IS POSITIVE) is evaluated, giving some intermediate truth value, t3. If t3 is false, the combined condition is false. If t3 is true, the combined condition is true.
Start of change

Example

The following example depicts an IF statement with two conditions combined with the logical AND operator.

IDENTIFICATION DIVISION.
PROGRAM-ID. COMBINE.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 N1 PIC 9(2) VALUE 30.
01 N2 PIC 9(2) VALUE 45.
01 N3 PIC 9(2) VALUE 30.

PROCEDURE DIVISION.
A000-FIRST-PARA.

   IF N1 IS LESS THAN N2 AND N1 = N3 THEN
         DISPLAY 'BOTH CONDITIONS OK'
   ELSE
         DISPLAY 'EITHER OR BOTH CONDITIONS FAILED'
   END-IF.

   STOP RUN.
The example produces the following output:
BOTH CONDITIONS OK
End of change