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:

  1. IF Statement
  2. EVALUATE Statement
  3. Nested Conditional Statements
  4. Practical Examples
  5. Exercises

  1. 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:

IF condition
    THEN
        statement-1
    ELSE
        statement-2
END-IF.

Example:

IF AGE > 18
    THEN
        DISPLAY 'You are an adult.'
    ELSE
        DISPLAY 'You are a minor.'
END-IF.

Explanation:

  • IF AGE > 18: Checks if the value of AGE is greater than 18.
  • THEN: If the condition is true, the statement following THEN is executed.
  • ELSE: If the condition is false, the statement following ELSE is executed.
  • END-IF: Marks the end of the IF statement.

  1. 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 the EVALUATE statement.

  1. 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 if AGE is greater than 18.
  • The nested IF checks if AGE is less than 60.
  • Depending on the conditions, the appropriate message is displayed.

  1. Practical Examples

Example 1: Simple IF Statement

IF GRADE >= 50
    THEN
        DISPLAY 'Pass'
    ELSE
        DISPLAY 'Fail'
END-IF.

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.

  1. 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.

© Copyright 2024. All rights reserved