Summary -
In this topic, we described about the below sections -
IF statement within another IF statement is called as Nested IF statement. Similarly IF ELSE statement within another IF ELSE statement is Nested IF ELSE statement. Multiple levels of validations are performed in Nested IF/IF ELSE Statement.
ENDIF scope terminator is mandatory with every IF statement. Each statement in nested IF/IF..ELSE statement should be end with period(.) including all IF, ELSE and ENDIF.
Syntax -
IF <logical-expression1>.
.
Statements-set1.
.
IF <logical-expression2>.
.
Statements-set2.
.
ELSE.
.
Statements-set3.
.
ENDIF.
ELSE.
.
Statements-set4.
.
IF <logical-expression3>.
.
Statements-set5.
.
ELSE.
.
Statements-set6.
.
ENDIF.
ENDIF.
Statement-set7.
Below diagram describes the flow of NESTED IF statement -
If the condition-1 is determined as true, then the statements-set1 executed. Next, the control goes to next IF condition condition-2. If the condition-2 is determined as true, then statements-set2 executed. Next, the control transfers to statements-set7. If the condition-2 is determined as false, then statements-set3 executed. Next, the control transfers to statements-set7.
If the condition-1 is determined as false, then the statements-set4 executed. Next, the control goes to next IF condition condition-3. If the condition-3 is determined as true, then statements-set5 executed. Next, the control transfers to statements-set7. If the condition-3 is determined as false, then statements-set6 executed. Next, the control transfers to statements-set7.
Example -
Below example shows how the NESTED IF statement coded in the application program.
Code -
*&---------------------------------------------------------------------*
*& Report Z_NESTED_IF
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*
REPORT Z_NESTED_IF.
* Declaring Variables
DATA: W_OP1 TYPE I VALUE 90,
W_OP2 TYPE I VALUE 70.
* IF condition to verify both variables Greater than equal or not
IF W_OP1 GE W_OP2.
* IF condition to verify both variables are equal or not
IF W_OP1 EQ W_OP2.
* If both variables are equal, displays below message.
WRITE 'W_OP1 EQUAL TO W_OP2'.
ELSE.
* If W_OP1 GREATER THAN W_OP2, displays below message.
WRITE 'W_OP1 GREATER THAN W_OP2'.
ENDIF.
ELSE.
* If W_OP1 LESS THAN W_OP2, displays below message.
WRITE 'W_OP1 LESS THAN W_OP2'.
ENDIF.
Output -
Explaining Example -
In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.
IF W_OP1 GE W_OP2, verifies W_OP1 Greater than or equal to W_OP2 or not. If condition is true, then control moves to the inner IF statement coded.
IF W_OP1 EQ W_OP2, verifies W_OP1 equals to W_OP2 or not. If condition is true, displays 'W_OP1 EQUAL TO W_OP2' message. Otherwise, 'W_OP1 GREATER THAN W_OP2' gets displayed.
IF W_OP1 GE W_OP2 is false, 'W_OP1 LESS THAN W_OP2' gets displayed.