Conditional Statements in COBOL Language

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:

  1. IF Statement: The basic IF statement is used to evaluate a condition and execute a block of code if the condition is true. It is often followed by an ELSE clause for an alternative block of code to execute if the condition is false. An END-IF statement 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
  1. EVALUATE Statement: The EVALUATE statement 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 a switch-case or switch statement 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
  1. ELSE and WHEN OTHER: The ELSE clause in an IF statement or the WHEN OTHER condition in an EVALUATE statement is used to specify the code to be executed when none of the previous conditions are true.
  2. Comparisons: Conditions in COBOL are often created using comparison operators such as =, NOT =, <, >, <=, >=, IS NUMERIC, IS ALPHABETIC, and more. You can use logical operators like AND, OR, and NOT to 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.

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading