COBOL Conditional Statements

A conditional statement derives a condition's truth value (TRUE or FALSE), and decides the further program execution flow. These statements allow decision-making in the program based on the evaluation of conditions.

A conditional statement can be divided into either a simple conditional statement (such as IF or EVALUATE) or a conditional statement that consists of an imperative statement that includes a conditional expression.

We can end a conditional statement with either an implicit scope terminator (.) or an explicit scope terminator (END-IF or END-EVALUATE). An explicit terminator converts the conditional statement into a delimited scope terminator (END-IF or END-EVALUATE).

The conditional statements are -

StatementDescription
IF Statement Detailed Tutorial   IF statement validates a condition to get the truth value and executes the statements under IF when the condition is TRUE. IF statements are of three types based on their usage in the program -
  • Simple IF
  • IF...ELSE
  • Nested IF
EVALUATE Statement Detailed Tutorial   EVALUATE validates multiple conditions at a time and controls the program flow. It is a shorter form for the nested IF...ELSE and simplifies the logic when multiple choices are available. EVALUATE logically divided into the below types based on their usage in the program -
  • Simple EVALUATE
  • EVALUATE TRUE
  • EVALUATE with THRU
  • EVALUATE with grouping multiple WHEN conditions
  • EVALUATE...WHEN with combined conditions
Conditional Expressions Detailed Tutorial   Conditional expression is a condition or a part of a condition that evaluates either TRUE or FALSE. These are used to choose the execution flow based on the truth value of the condition.
They are -
  • CLASS Condition - CLASS condition tests whether the data in a variable belongs to a specific category of characters such as ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER, NUMERIC, etc.
  • Conditional Names - is a meaningful name for a value or a range of values of a variable. IF and EVALUATE statements validate whether the condition variable's value is equal to any values associated with the condition name.
  • Relation Condition - compares two operands using relational operators (=, >, <, <=, >=, <>) and returns a boolean value of TRUE or FALSE.
  • SIGN Condition - tests the sign (positive, negative, or zero) of the data in numeric variables.
  • Combined (AND|OR) Condition - is a logical group with two or more conditions and is used to test multiple conditions to produce a single decision.
  • Negated Condition - A condition is negated with the use of the logical operator NOT.

Some statements only work with conditional statements, and those are -

StatementDescription
CONTINUE Detailed Tutorial   CONTINUE statement transfers the control to the immediate COBOL statement that comes next in the program flow. It is a no-operation, and it is a do-nothing statement.
NEXT SENTENCE Detailed Tutorial   NEXT SENTENCE transfers the control to the following COBOL statement immediately after the explicit scope terminator (period - '.') in the flow.

Statements with conditional phrases -

If any statement uses the SIZE ERROR, OVERFLOW, AT END, INVALID KEY, AND EXCEPTION phases, those are also called conditional statements. Below COBOL statements become conditional when a condition is coded with them -