Conditional Statement in COBOL
Introduction
- The result of conditional statements are always true or false.
- These statements change the flow of execution depending on the certain conditions defined by the programmer.
Types of conditional statements
Following are the various types of conditional statements:
1) IF condition statement
2) Relation condition
3) Sign condition
4) Class condition
5) Condition-name condition
6) Navigate condition
7) Combined condition
IF condition statement
- This statement checks the condition. If the condition is true, then IF block is executed. If the condition is false, then the ELSE block is executed.
- To end the IF block END-IF is used. Instead of END-IF a period can be used.
- The use of END-IF is always preferable for the multiple IF blocks.
- If the IF block is occurring inside another IF block, then it is known as nested IF.
Syntax:
IF [condition] THEN
[COBOL statements]
ELSE
[COBOL statements]
END-IF
Example : Program to demonstrate IF condition statement
IDENTIFICATION DIVISION.
PROGRAM-ID. ICS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 N1 PIC 9(3).
01 N2 PIC 9(3).
01 N3 PIC 9(4).
01 N4 PIC 9(4).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 30 TO N1 N3.
MOVE 12 TO N2 N4.
IF N1 > N2 THEN
DISPLAY 'IF BLOCK OF LOOP 1'
IF N3 = N4 THEN
DISPLAY 'IF BLOCK OF IN LOOP 2'
ELSE
DISPLAY 'ELSE BLOCK OF IN LOOP 2'
END-IF
ELSE
DISPLAY 'ELSE BLOCK OF IN LOOP 1'
END-IF.
STOP RUN.
Output:
IF BLOCK OF LOOP 1
ELSE BLOCK OF IN LOOP 2