Conditional Statements in COBOL
Last Updated :
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 :
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:
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
Class condition:
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 condition:
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
Negated Condition:
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.
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
Compound Condition:
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 condition:
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 condition:
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
Similar Reads
COBOL - Continue Statement
In simple terms think of the continue statement as a Gatekeeper who will open the gate for the people to come inside the premise to do their task. Similarly continue statement also transfers the control on the next COBOL statement for execution. Syntax: CONTINUE Let's take the example of Continue in
2 min read
Coding Sheet in COBOL
Every language needs an environment or platform to write codes. For example, in Java, we use notepad to write codes then compile them to run. Similarly, COBOL requires a coding sheet to write codes. COBOL is a business-oriented high-level language. It was developed for business, finance, and adminis
7 min read
Loop Statements in COBOL
Every programming language not only has a selection construct but also one that involves iteration. With this construct, it is possible for a block of code to run repeatedly. In fact, the programmer himself can code it in by selecting a specific type of loop. Speaking of types of loops, modern prog
8 min read
Compound Statements in C++
Compound statements in C++ are blocks used to group multiple statements together into a single unit. These statements are enclosed in curly braces {} and can be used wherever a single statement is expected. The statements put inside curly braces are executed just like a single statement would have b
3 min read
COBOL - Copy Statement
COPY statement is used to bring into a program a series of prewritten COBOL entries that have been stored in a library. A copy statement inserts the copybook inside a COBOL program, copybook refers to a member which holds all the variables declared inside it. The COPY statement is a library statemen
2 min read
COBOL - Include Statement
The INCLUDE statement refers to the file or the directory mentioned after it and inserts the command present inside that member in the pre-compilation state. Basically, it is used to insert a code block onto a source program. Syntax: INCLUDE member-name Where, The member-name refers to the name of t
2 min read
Conditional Statements in Ansible: A Practical Guide
Ansible is a significant player in the field of IT automation and configuration management, considering its simplicity and flexibility, the flexibility of Ansible is enhanced with the help of conditional statements. Conditional statements in Ansible control the flow of tasks in your playbook, ensuri
9 min read
SAP ABAP | Decision Control Statements
SAP ABAP, or Advanced Business Application Programming, provides a robust set of tools for developing business applications within the SAP environment. Decision control statements are a crucial aspect of programming logic, allowing developers to make choices and guide the flow of a program based on
3 min read
Control flow statements in Programming
Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow St
15+ min read
Compound Statements
Statements are tools used in logical reasoning and problem-solving. Compound statement, for instance, not only unites simple statements into one big complex but also still maintains its meaning. This article will examine the compound statements: what they are, their types, and the truth tables for e
8 min read