COBOL - Conditional StatementsDepending 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 StatementIf 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 IFSimple 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 ELSEIF 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 IFLike 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: Example 2:Let's see another simple example for IF condition statement: Output: Relation ConditionThe 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:
Syntax:Example:Output: Sign ConditionSign 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: Class ConditionClass condition checks if an operand contains only alphabets or numeric data. Whitespaces are considered in ALPHABETIC, ALPHABETIC-LOWER, and ALPHABETIC-UPPER. Syntax:Example:Output: Condition-name ConditionCondition-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: Negated ConditionNegated 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: Combined ConditionA combined condition statement includes two or more conditions associated with OR or AND logical operators. Syntax:Example:Output: Evaluate VerbWe 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: Next TopicCOBOL Loop Statements |