![]() |
VOOZH | about |
While writing a program a programmer needs to check for various conditions, if the condition is true then a particular block of statement/s are executed, or else another block of statement/s is execute. To check these conditions we use Conditional Statements. These statements return either true or false. There are various types of Conditional statements in Cobol :
If the programmer wants to make a relational comparison between operand/s or literal/s then we use relational operators i.e. < , > , < = , >= , ==. We can either use the particular relational symbol or we can spell it out in words.
For example :
Syntax:
Operand-1 [IS][NOT] relational-operator Operand-2.
Example:
Output:
OPERAND1 IS GREATER THAN OPERAND2 OPERAND3 AND OPERAND4 ARE EQUAL
Sign-condition determines whether a particular operand or arithmetic expression is Positive, Negative, or Zero. In the Sign condition, the Positive condition is determined to be true only when the value of the operand is strictly true i.e. the value Zero is not treated positively in this case.
Syntax:
Operand1/Arithmetic expression IS [NOT] POSITIVE/NEGATIVE/ZERO
Example:
Output:
OPERAND1 IS POSITIVE OPERAND2 IS NOT ZERO
If the programmer wants to check whether an entered value is alphabet or number then we use Class Conditions. It helps to check whether the entered value is valid or not. The numeric condition is true only when the entered value contains digits from 0-9,(irrespective of sign) or is alphanumeric. Similarly, the alphabetic condition is true only when the entered value contains alphabets from A-Z or is alphanumeric.
Syntax:
Operand IS [NOT] NUMERIC/ALPHABETIC
Example:
Output:
ENTER ANY ALPHABET/NUMBER C ALPHABET
Condition-name is a condition itself and contains either true or false values. The condition-name must always be declared within a data name called Conditional Variable, i.e. it cannot be defined independently. The condition-name becomes true only if the conditional variable assumes any of the assigned values. The LEVEL 88 is used to declare condition-name.
For example;
77 EXPERIENCE PIC 9. 88 BEGINNER VALUE IS 1. 88 INTERMEDIATE VALUE IS 3. 88 PROFESSIONAL VALUES ARE 5,6,7.
Explanation: In the above example "EXPERIENCE" is a Conditional Variable with level 77, whereas, "BEGINNER ", "INTERMEDIATE", "PROFESSIONAL" are Condition-name with level 88. Now the BEGINNER condition will be true only when the value of EXPERIENCE is 1 and similarly, the INTERMEDIATE condition will be true only when the value of EXPERIENCE is 3, and the PROFESSIONAL condition will be true only when the value of EXPERIENCE is either 5 or 6 or 7.
Syntax:
77 Conditional Variable PIC 9/A/X.
88 Condition-name1 {VALUE IS/VALUES ARE} Literal-1 [ {THRU/THROUGH} Literal-2][,Literal-3 [{THRU/THROUGH} Literal-4]]
88 Condition-name2 {VALUE IS/VALUES ARE} Literal-1 [ {THRU/THROUGH} Literal-2][,Literal-3 [{THRU/THROUGH} Literal-4]]
Example:
Output:
ENTER EXPERIENCE 7 INTERMEDIATE LEVEL
Any condition that has a NOT operator preceding it is known as Negated Condition. This condition always produces the reverse of the condition, i.e. if the condition is true the final output will be false and vice versa. The NOT operator can be used in two ways:
Syntax:
NOT Condition
Example:
Output:
FALSE CONDITION REVERSED
When the programmer has to check two or more conditions as a single condition then we use logical operators, AND or OR, two combine the conditions. In the case of AND, the compound condition returns a true value only if all the constituent conditions are true, else it returns false. In the case of OR, the compound condition returns true value if any one of the constituent conditions is true, else it returns false.
Syntax:
Condition1 AND/OR Condition2 [AND/OR Condition3....];
Example:
Output:
BOTH OPERANDS ARE NOT POSITIVE
If conditional statement checks for conditions, if a particular condition returns a true value only then a particular set of statements are executed. These statements can be simple statements or compound statements. If the condition returns a false value then the control does not go into the IF-block. In this case always remember that PERIOD(.) will only be used at the end of the last statement, to terminate the IF-block.
Syntax:
IF (Condition/s) Set of statements [END IF]
Example:
Output:
OPERAND1 IS GREATER
If-else conditional statement checks for the condition/s, if the condition is true then it executes the statements of IF-block, if the condition is false then it executes the statement of ELSE-block. In this case always remember that PERIOD(.) will only be used at the end of the last statement, to terminate the IF-ELSE block.
Syntax:
IF (Condition/s) Set of Statements ELSE Set of statements [END IF].
Example:
Output:
OPERAND2 IS GREATER
The below syntax is also used for the nested if-condition statements in COBOL:
Syntax:
IF (Condition1) IF (Condition2) Set of statement/s ELSE Set of statement/s END-IF ELSE Set of statement/s END-IF.
Example:
Output:
OPERAND IS NUMERIC OPERAND IS POSITIVE