Conditional Statements in COBOL Language
Conditional statements in COBOL are used to control the flow of a program based on certain conditions. These statements allow you to make decisions and execute different blocks of code depending on whether a specified condition is true or false. In COBOL, conditional statements are typically implemented using the IF-ELSE-END-IF construct.
Here’s an overview of conditional statements in COBOL:
- IF Statement: The basic
IFstatement is used to evaluate a condition and execute a block of code if the condition is true. It is often followed by anELSEclause for an alternative block of code to execute if the condition is false. AnEND-IFstatement marks the end of the conditional block.
IF condition
Perform this block of code
ELSE
Perform this block of code if the condition is false
END-IF
- EVALUATE Statement: The
EVALUATEstatement is used when there are multiple conditions to be evaluated, and you want to choose one of several code blocks based on the condition that is true. It is similar to aswitch-caseorswitchstatement in other languages.
EVALUATE True-Condition
WHEN Condition-1
Perform this block of code
WHEN Condition-2
Perform this block of code
WHEN OTHER
Perform this block of code if none of the above conditions are true
END-EVALUATE
- ELSE and WHEN OTHER: The
ELSEclause in anIFstatement or theWHEN OTHERcondition in anEVALUATEstatement is used to specify the code to be executed when none of the previous conditions are true. - Comparisons: Conditions in COBOL are often created using comparison operators such as
=,NOT =,<,>,<=,>=,IS NUMERIC,IS ALPHABETIC, and more. You can use logical operators likeAND,OR, andNOTto create more complex conditions.
Here are some examples of conditional statements in COBOL:
Example 1: Simple IF-ELSE Statement:
IF Age > 18
DISPLAY 'You are an adult.'
ELSE
DISPLAY 'You are a minor.'
END-IF.
Example 2: EVALUATE Statement:
EVALUATE Score
WHEN 90 THRU 100
DISPLAY 'You got an A.'
WHEN 80 THRU 89
DISPLAY 'You got a B.'
WHEN 70 THRU 79
DISPLAY 'You got a C.'
WHEN OTHER
DISPLAY 'You need to study harder.'
END-EVALUATE.


