Conditional Expressions


The conditional expression is a condition or a part of a condition that evaluates to either TRUE or FALSE. These are used to choose the execution flow based on the truth value of the condition.

Conditional expressions are used in decision-making statements, IF, IF...ELSE, EVALUATE, and in loop statements, PERFORM and SEARCH statements.

The conditional expression can be either a simple condition or a complex condition. Simple and complex conditions are enclosed with parentheses based on need.

Simple conditions -


A simple condition compares two operands using an operator. There are five types of simple conditions -

Class Condition

Class condition checks if a data item belongs to a specific class like NUMERIC, ALPHABETIC, ALPHABETIC-LOWER, or ALPHABETIC-UPPER. For example -

IF WS-VAR IS NUMERIC
   ... 
END-IF.

Condition name

Condition name refers to the 88-level condition name that verifies whether variable value equals to the associated with the condition name. For example -

01 WS-VAR        PIC X(01).
   88 WS-MALE    VALUE "M".
   ...
IF WS-MALE
   ... 
END-IF.

Relation Condition

The relation condition compares two operands using relational operators (=, >, <, <=, >=, <>). For example -

IF WS-VAR1 = WS-VAR2
   ... 
END-IF.

Sign Condition

The sign condition checks for the sign of a numeric variable. For example -

IF WS-VAR IS NEGATIVE
   ... 
END-IF.

Combined conditions


A Combined condition combines two or more simple conditions using AND or OR. Each logical operator must be preceded and followed by a space.

AND Condition

It combines two or more simple conditions using AND. The truth value is true when both conditions are true. Example -

IF (WS-A > WS-B) AND (WS-C < WS-D)
   ... 
END-IF.

OR Condition

It combines two or more simple conditions using OR. The truth value is true when either or both conditions are true. Example -

IF (WS-A > WS-B) OR (WS-C < WS-D)
   ... 
END-IF.

NOT Condition -

A simple condition is negated with the use of the logical operator NOT. The negated simple condition gives the opposite truth value of the simple condition. For Example, if the truth value of the condition is true, then the truth value of same negated simple condition is false and vice versa.

IF NOT (WS-A = WS-B)
   ... 
END-IF.

Truth Table


Let us assume C1 is the condition1 and C2 is the condition2. The table below shows the true value of AND, OR, and NOT logical operators -

C1C2C1 AND C2C1 OR C2NOT C1NOT C2
True True True True False False
False True False True True False
True False False True False True
False False False False True True

Operators precedence -


The following is the operator order of precedence from highest to lowest -

  • Arithmetic operators.
  • Simple conditions.
  • NOT
  • AND
  • OR