IF Statement
- IF statement validates a condition and decides the program flow depending on the condition validation.
- It comes under selective programming.
- IF statements are 3 formats based on their usage in the program -
- Simple IF
- IF ELSE
- Nested IF
Simple IF -
A simple IF statement is used to execute the set of statements (statements-block-1) only if condition-1 is true.
Syntax -
IF condition-1 [THEN]
statements-block-1 | NEXT SENTENCE
[END-IF].
statements-block-n.
If the condition-1 is true, then the statements-block-1 gets executed. If the condition-1 is flase, the control transfers to the statements-block-n which are immediately after IF statement.
Parameters -
- condition-1, ... - Specifies conditions. A condition can be a simple, compound, class, condition-name, or sign condition.
- statements-block-1, ... - These are the COBOL statements that get executed when the associated condition is true.
- THEN - This is an optional keyword that can be used after a condition for clarity, but it's not required by most COBOL compilers.
- NEXT SENTENCE - It transfers control to statement immediately following the period.
- END-IF - It marks the end of the IF statement. If a period (.) can be coded at the last statement of the IF block, END-IF is not needed.
Example -
Scenario - Validating gender
IF WS-GENDER EQUAL 'M'
DISPLAY "Person is Male"
END-IF.
IF ELSE -
IF...ELSE condition is used to validate a condition and handles both (either TRUE or FALSE) outcomes of the condition.
Syntax -
IF condition-1 [THEN]
statements-block-1 | NEXT SENTENCE
[ELSE]
statements-block-2 | NEXT SENTENCE}
[END-IF].
statements-block-n.
If the condition-1 is true, the statements-block-1 gets executed first, and control transfers to statements-block-n. If the condition-1 is false, the statements-block-2 gets executed first, and control transfers to statements-block-n.
Example -
Scenario - Validating gender
IF WS-GENDER EQUAL 'M'
DISPLAY "Person is Male"
ELSE
DISPLAY "person is Female"
END-IF.
NESTED IF -
IF statement coded within another IF statement is called as NESTED IF statement. In Nested IF, all IF statements should have their matching END-IF.
Syntax -
IF condition-1 [THEN]
IF condition-2 [THEN]
statements-block-1
[ELSE
statements-block-2
END-IF]
[ELSE
IF condition-3 [THEN]
statements-block-3
[ELSE
statements-block-4
END-IF]
END-IF].
statements-block-n.
If condition-1 is true, the condition-2 is evaluated. If condition-2 is also true, statements-block-1 gets executed first, and control transfers to statements-block-n. If condition-2 is false, statements-block-2 gets executed first, and control transfers to statements-block-n.
If condition-1 is false, the control transfers to the ELSE part and validates condition-3. If condition-3 is true, statements-block-3 gets executed first, and control transfers to statements-block-n. If condition-3 is false, statements-block-4 get executed first, and control transfers to statements-block-n.
Practical Example -
Scenario - Coding all types of IF conditions.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-NAME PIC X(20).
88 WS-NAME-VALID VALUE 'A' THRU 'Z'.
05 WS-GENDER PIC X(01).
88 WS-MALE VALUE 'M'.
88 WS-FEMALE VALUE 'F'.
05 WS-MARKS-PERCENT PIC 9(03).
PROCEDURE DIVISION.
* Receiving data from JCL SYSIN
ACCEPT WS-NAME.
ACCEPT WS-GENDER.
ACCEPT WS-MARKS-PERCENT.
* IF statement
IF WS-NAME-VALID
DISPLAY 'VALID NAME: ' WS-NAME
END-IF.
* IF..ELSE statement
IF WS-MALE
DISPLAY 'PERSON IS MALE'
ELSE
DISPLAY 'PERSON IS FEMALE'
END-IF.
* Nested IF..ELSE statement
IF WS-MARKS-PERCENT > 60
DISPLAY 'GOT FIRST CLASS'
ELSE
IF WS-MARKS-PERCENT > 50
DISPLAY 'GOT SECOND CLASS'
ELSE
IF WS-MARKS-PERCENT > 35
DISPLAY 'GOT THIRD CLASS'
ELSE
DISPLAY 'FAILED'
END-IF
END-IF
END-IF.
...
Run JCL -
//MATEPKRJ JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID
//*
//STEP01 EXEC PGM=IFCOND
//STEPLIB DD DSN=MATEPK.COBOL.LOADLIB,DISP=SHR
//SYSIN DD *
SRINIVAS
M
90
/*
//SYSOUT DD SYSOUT=*
Output -
VALID NAME: SRINIVAS PERSON IS MALE GOT FIRST CLASS