Conditional Statement in COBOL
- 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 statementsFollowing 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.
IF [condition] THEN
Example : Program to demonstrate IF condition statement
01 N1 PIC 9(3).
01 N2 PIC 9(3).
01 N3 PIC 9(4).
01 N4 PIC 9(4).
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'
DISPLAY 'ELSE BLOCK OF IN LOOP 2'
DISPLAY 'ELSE BLOCK OF IN LOOP 1'
IF BLOCK OF LOOP 1
ELSE BLOCK OF IN LOOP 2