COBOL - Conditional Statements

Depending on certain conditions specified by the programmer, conditional statements are used to change the execution flow. The output is always given in true or false by a conditional statement. COBOL contains various types of conditional statements:

If Condition Statement

If condition statement is used to check for a condition if a condition is true, the IF block is executed, and if the condition is false, the ELSE block is executed.

IF statement mainly has three types based on its usage in the COBOL program:

Simple IF

Simple IF is mainly used to execute the condition related code. If the condition is true, then it will execute the set of statements written in the IF block. If condition is not satisfied, the control will transfer to the next statements after the IF statement terminated.

Syntax:

Here, END-IF is the scope terminator, which is optional in the program. The period (.) can be defined at the last statement of IF block.

If we didn't specify the period, then scope terminator END-IF is mandatory.

IF ELSE

IF ELSE statement is used when a certain set of statements needed to be executed by two conditions. This statement is mainly used to execute the condition-specific code.

In IF-ELSE, the block of statements will be executed if the specified condition is true. If the condition is false, the other set of statements will be executed, and those set will be under the ELSE block.

Syntax:

Nested IF

Like other COBOL programming languages, COBOL also allows the nested IF statement. IF statement within the IF statement called as nested IF statement. There is no bound to the depth of nested IF statements.

Syntax:

Example 1:

Let's see an example for IF condition statement in the COBOL program:

Output:

COBOL Conditional Statements

Example 2:

Let's see another simple example for IF condition statement:

Output:

COBOL Conditional Statements

Relation Condition

The relation condition contrasts two operands. These operands can be an identifier, literal, or arithmetic expression.

A comparison of algebraic numeric fields is conducted regardless of size and usage clause.

For Non-numeric operands

By comparing two non-numeric operands of equal size, the characters will be measured from the left with the corresponding positions until the end is reached. The operand with more characters is considered to be larger.

While comparing two operands of different sizes, the shorter data item will be appended with spaces at the end until the operand's size is equal and then compared according to the rules stated in the preceding paragraph.

The Relational operator defines the type of comparison to be made as given below:

Relational OperatorWe can write asDescription
IS EQUAL TOIS =It means, Equal to
IS GREATER THANIS >It means, Greater than
IS LESS THANIS <It means, Less than
IS NOT GREATER THANIS NOT >It means, Not greater than
IS NOT LESS THANIS NOT <It means, Not less than
IS NOT EQUAL TOIS NOT =It means, Not equal to
IS GREATER THAN OR EQUAL TOIS >=It means, Is greater than or equal to
IS LESS THAN OR EQUAL TOIS <=It means, Is less than or equal to

Syntax:

Example:

Output:

COBOL Conditional Statements

Sign Condition

Sign condition checks the sign of a numeric operand. It is used to decide if a given numerical value is greater than, lower than, equal to, or ZERO.

Syntax:

Example:

Output:

COBOL Conditional Statements

Class Condition

Class condition checks if an operand contains only alphabets or numeric data. Whitespaces are considered in ALPHABETIC, ALPHABETIC-LOWER, and ALPHABETIC-UPPER.

Syntax:

Example:

Output:

COBOL Conditional Statements

Condition-name Condition

Condition-name is a name defined by the user. This statement contains a set of user-defined values and acts as Boolean variables. The level-88 defines them. It won't have a PIC clause.

Syntax:

Example:

Output:

COBOL Conditional Statements

Negated Condition

Negated condition is given by using the keyword NOT. If a condition is satisfied and we have given NOT in front of it, its final value will be false.

Syntax:

Example:

Output:

COBOL Conditional Statements

Combined Condition

A combined condition statement includes two or more conditions associated with OR or AND logical operators.

Syntax:

Example:

Output:

COBOL Conditional Statements

Evaluate Verb

We can use the Evaluate verb as a replacement for a series of IF-ELSE statements. It works the same as the SWITCH statement of C programs. This verb is very useful in evaluating more than one condition.

During the program execution, it offers multiple-selection control. In a single shot, it can test multiple conditions.

Syntax:

Example:

Output:

COBOL Conditional Statements




Latest Courses