Conditional statements in COBOL allow you to control the flow of execution based on certain conditions. These statements are essential for making decisions within your program. In this section, we will cover the following topics:
- IF Statement
- EVALUATE Statement
- Nested Conditional Statements
- Practical Examples
- Exercises
- IF Statement
The IF
statement is used to execute a block of code only if a specified condition is true. The basic syntax is as follows:
Example:
Explanation:
IF AGE > 18
: Checks if the value ofAGE
is greater than 18.THEN
: If the condition is true, the statement followingTHEN
is executed.ELSE
: If the condition is false, the statement followingELSE
is executed.END-IF
: Marks the end of theIF
statement.
- EVALUATE Statement
The EVALUATE
statement is similar to the SWITCH
statement in other programming languages. It allows you to evaluate multiple conditions and execute corresponding blocks of code.
Syntax:
EVALUATE TRUE WHEN condition-1 statement-1 WHEN condition-2 statement-2 WHEN OTHER statement-3 END-EVALUATE.
Example:
EVALUATE TRUE WHEN AGE < 13 DISPLAY 'Child' WHEN AGE >= 13 AND AGE < 20 DISPLAY 'Teenager' WHEN AGE >= 20 AND AGE < 60 DISPLAY 'Adult' WHEN OTHER DISPLAY 'Senior' END-EVALUATE.
Explanation:
EVALUATE TRUE
: Starts the evaluation.WHEN condition
: Specifies a condition to be evaluated.WHEN OTHER
: Specifies the default case if none of the conditions are met.END-EVALUATE
: Marks the end of theEVALUATE
statement.
- Nested Conditional Statements
You can nest IF
statements within other IF
statements or within EVALUATE
statements to handle more complex conditions.
Example:
IF AGE > 18 THEN IF AGE < 60 THEN DISPLAY 'You are an adult.' ELSE DISPLAY 'You are a senior.' END-IF ELSE DISPLAY 'You are a minor.' END-IF.
Explanation:
- The outer
IF
checks ifAGE
is greater than 18. - The nested
IF
checks ifAGE
is less than 60. - Depending on the conditions, the appropriate message is displayed.
- Practical Examples
Example 1: Simple IF Statement
Example 2: EVALUATE Statement
EVALUATE TRUE WHEN GRADE >= 90 DISPLAY 'A' WHEN GRADE >= 80 DISPLAY 'B' WHEN GRADE >= 70 DISPLAY 'C' WHEN GRADE >= 60 DISPLAY 'D' WHEN OTHER DISPLAY 'F' END-EVALUATE.
- Exercises
Exercise 1: Basic IF Statement
Write a COBOL program that checks if a number is positive, negative, or zero and displays the appropriate message.
Solution:
IDENTIFICATION DIVISION. PROGRAM-ID. CheckNumber. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUMBER PIC S9(4). PROCEDURE DIVISION. DISPLAY 'Enter a number:' ACCEPT NUMBER IF NUMBER > 0 THEN DISPLAY 'The number is positive.' ELSE IF NUMBER < 0 THEN DISPLAY 'The number is negative.' ELSE DISPLAY 'The number is zero.' END-IF END-IF. STOP RUN.
Exercise 2: EVALUATE Statement
Write a COBOL program that categorizes a person's age into different life stages: Child, Teenager, Adult, and Senior.
Solution:
IDENTIFICATION DIVISION. PROGRAM-ID. AgeCategory. DATA DIVISION. WORKING-STORAGE SECTION. 01 AGE PIC 99. PROCEDURE DIVISION. DISPLAY 'Enter your age:' ACCEPT AGE EVALUATE TRUE WHEN AGE < 13 DISPLAY 'Child' WHEN AGE >= 13 AND AGE < 20 DISPLAY 'Teenager' WHEN AGE >= 20 AND AGE < 60 DISPLAY 'Adult' WHEN OTHER DISPLAY 'Senior' END-EVALUATE. STOP RUN.
Conclusion
In this section, we covered the basics of conditional statements in COBOL, including the IF
and EVALUATE
statements. We also explored nested conditional statements and provided practical examples and exercises to reinforce the concepts. Understanding these control structures is crucial for making decisions in your COBOL programs. In the next section, we will delve into looping constructs, which allow you to execute a block of code multiple times based on certain conditions.