Conditional Statements in COBOL - GeeksforGeeks

24 Feb, 2022
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 :
Relational condition.Sign condition.Class condition.Condition-name condition.Negated condition.Compound condition.If condition.If-else condition.Nested If condition.
Relational condition.
Sign condition.
Class condition.
Condition-name condition.
Negated condition.
Compound condition.
If condition.
If-else condition.
Nested If condition.
 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 : 
(a > b ) can also be written as  (a GREATER THAN b).
(a !< b) can also be written as (a IS NOT LESS THAN b).
(a == b) can also be written as (a IS EQUAL TO b).
Syntax:
Operand-1 [IS][NOT] relational-operator Operand-2.
Example:
Cobol
IDENTIFICATION DIVISION.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    77 OPERAND1 PIC 99.    77 OPERAND2 PIC 99.    77 OPERAND3 PIC 99.    77 OPERAND4 PIC 99.PROCEDURE DIVISION.    MOVE 8 TO OPERAND1.    MOVE 5 TO OPERAND2.    MOVE 4 TO OPERAND3.    MOVE 4 TO OPERAND4.    IF (OPERAND1 > OPERAND2)            DISPLAY "OPERAND1 IS GREATER THAN OPERAND2"    ELSE            DISPLAY "OPERAND2 IS GREATER THAN OPERAND2".    IF (OPERAND3 IS EQUAL TO OPERAND4)            DISPLAY "OPERAND3 AND OPERAND4 ARE EQUAL"    ELSE            DISPLAY "OPERAND3 AND OPERAND4 ARE NOT EQUAL".    STOP RUN.
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:
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. HELLOWORLD.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    77 OPERAND1 PIC 99.    77 OPERAND2 PIC 99.PROCEDURE DIVISION.    MOVE 8 TO OPERAND1.    MOVE 5 TO OPERAND2.    IF (OPERAND1 IS POSITIVE)            DISPLAY "OPERAND1 IS POSITIVE".    IF (OPERAND2 IS NOT ZERO)            DISPLAY "OPERAND2 IS NOT ZER0".    STOP RUN.
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:
Cobol
IDENTIFICATION DIVISION.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    77 OPERAND PIC X9.PROCEDURE DIVISION.    DISPLAY "ENTER ANY ALPHABET/NUMBER".    ACCEPT OPERAND.    IF (OPERAND IS ALPHABETIC)            DISPLAY "ALPHABET"    ELSE            DISPLAY "NUMBER".    STOP RUN.
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:
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. HELLOWORD.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.      77 WEXP PIC 99.      88 PROFESSIONAL VALUE 10.      88 HIGH VALUE 9.      88 INTERMEDIATE VALUES ARE 6,7,8.      88 BEGINNER VALUES ARE 1 THRU 5.      88 UNEXP VALUE IS 0.PROCEDURE DIVISION.      DISPLAY "ENTER EXPERIENCE".      ACCEPT WEXP.      IF PROFESSIONAL             DISPLAY "PROFESSIONAL".      IF HIGH             DISPLAY "HIGHLY EXPERIENCED".      IF INTERMEDIATE               DISPLAY "INTERMEDIATE LEVEL".      IF  BEGINNER               DISPLAY "BEGINNER".      IF UNEXP               DISPLAY "UNEXPERIENCED".STOP RUN.
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:
As a part of the condition, as we have seen in the above conditions.It can simply precede any condition and make it a Negated Condition.
As a part of the condition, as we have seen in the above conditions.
It can simply precede any condition and make it a Negated Condition.
Syntax:
NOT Condition
Example:
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. HELLOWORD.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    77 OPERAND1 PIC 99.    77 OPERAND2 PIC 99.PROCEDURE DIVISION.    MOVE 10 TO OPERAND1.    MOVE 8 TO OPERAND2.    IF NOT OPERAND1 > OPERAND2            DISPLAY "TRUE CONDITION REVERSED"    ELSE            DISPLAY "FALSE CONDITION REVERSED".    STOP RUN.
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:
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. HELLOWORD.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    77 OPERAND1 PIC 9.    77 OPERAND2 PIC 9.PROCEDURE DIVISION.    MOVE 7 TO OPERAND1.    MOVE 0 TO OPERAND2.    IF  OPERAND1 IS POSITIVE AND OPERAND2 IS POSITIVE            DISPLAY "BOTH OPERANDS ARE POSITIVE"    ELSE            DISPLAY "BOTH OPERANDS ARE NOT POSITIVE".    STOP RUN.
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:
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. HELLOWORD.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    77 OPERAND1 PIC 99.    77 OPERAND2 PIC 99.PROCEDURE DIVISION.    MOVE 10 TO OPERAND1.    MOVE 8 TO OPERAND2.    IF OPERAND1 > OPERAND2            DISPLAY "OPERAND1 IS GREATER".     STOP RUN.
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:
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. HELLOWORD.ENVIRONMENT DIVISION.DATA DIVISION.WORKING-STORAGE SECTION.    77 OPERAND1 PIC 99.    77 OPERAND2 PIC 99.PROCEDURE DIVISION.    MOVE 3 TO OPERAND1.    MOVE 8 TO OPERAND2.    IF OPERAND1 > OPERAND2            DISPLAY "OPERAND1 IS GREATER"    ELSE              DISPLAY "OPERAND2 IS GREATER".    STOP RUN.
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:
Cobol
IDENTIFICATION DIVISION.PROGRAM-ID. HELLOWORD.ENVIRONMENT DIVISION.DATA DIVISION.      WORKING-STORAGE SECTION.          77 OPERAND1 PIC 99.PROCEDURE DIVISION.      MOVE 12 TO OPERAND1.      IF OPERAND1 IS NUMERIC              DISPLAY "OPERAND IS NUMERIC"              IF OPERAND1 IS POSITIVE                  DISPLAY " OPERAND IS POSITIVE"              ELSE                  IF OPERAND1 IS NEGATIVE                      DISPLAY "OPERAND IS NEGATIVE"                  ELSE                      DISPLAY "OPERAND IS ZERO"                  END-IF              END-IF      ELSE              DISPLAY "OPERAND IS NOT NUMERIC"    END-IF.STOP RUN.
Output:
OPERAND IS NUMERIC
OPERAND IS POSITIVE
sagartomar9927
gabaa406
sumitgumber28
sagar0719kumar
Blogathon-2021
COBOL-Conditional-Expressions
Picked
Blogathon
COBOL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to parse JSON Data into React Table Component ?
How to Install Tkinter on MacOS?
How to validate form using Regular Expression in JavaScript ?
Explain the purpose of render() in ReactJS
How to write comments in ReactJS ?
Difference Between Sequential, Indexed, and Relative Files in COBOL
Difference between COMP and COMP3
Difference Between Search and Search All in COBOL
Loop Statements in COBOL
Variables in COBOL