The IF statement is the central control structure in ABAP for controlling program flow based on conditions. It enables one or more statement blocks to be executed only when a specific logical condition is satisfied (true). This allows you to create branches in the program flow to respond to different situations or data states.
Syntax
The IF statement comes in various forms:
1. Simple IF Condition (without Alternative)
IF <logical_expression>. " Statement block 1 " This block is only executed if <logical_expression> is true. [Statement1_1]. [Statement1_2]. ...ENDIF.IF: Initiates the condition check.<logical_expression>: A condition that evaluates to true (abap_true) or false (abap_false).ENDIF: Closes theIFblock.
2. IF Condition with Alternative (ELSE)
IF <logical_expression>. " Statement block 1 (If true) ...ELSE. " Statement block 2 (If false) ...ENDIF.ELSE: Initiates the block that is executed if the<logical_expression>in theIFpart was false.
3. IF Condition with Multiple Alternatives (ELSEIF) and Optional ELSE
IF <logical_expression_1>. " Statement block 1 (If expression 1 is true) ...ELSEIF <logical_expression_2>. " Statement block 2 (If expression 1 was false AND expression 2 is true) ...ELSEIF <logical_expression_3>. " Statement block 3 (If expressions 1 & 2 were false AND expression 3 is true) ...[ELSE. " Optional block for all other cases " Statement block N (If ALL previous expressions were false) ...]ENDIF.ELSEIF: Checks another condition, but only if all previousIFandELSEIFconditions were false.
Functionality and Flow
- Conditions are checked strictly from top to bottom (
IFfirst, then the firstELSEIF, then the second, etc.). - As soon as a condition is recognized as true (
abap_true), the associated statement block is executed. - Very important: After executing this one block, the program jumps directly to the statement after
ENDIF. No furtherELSEIFconditions or theELSEblock within the sameIF...ENDIFstructure are checked or executed. - If all
IFandELSEIFconditions are false (abap_false), theELSEblock is executed (if present). - If all conditions are false and there is no
ELSEblock, execution simply continues with the statement afterENDIF, without executing any of the blocks within theIFstructure.
Logical Expressions
The <logical_expression> can take various forms:
- Comparisons:
=,<>,<,>,<=,>=(e.g.,count > 0,text = 'Yes') - State checks:
var IS INITIAL,ref_var IS BOUND - Boolean variables:
is_ok = abap_true - Logical operators for combination:
AND: Both sub-expressions must be true.OR: At least one of the sub-expressions must be true.NOT: Reverses the result of the following expression.- Example:
IF count > 0 AND is_active = abap_true.
- Predicate functions: Functions that return
abap_trueorabap_false(e.g.,line_exists(...)).
Nesting
IF statements can be nested within each other to build more complex decision trees. Good indentation is essential for readability here.
IF condition1. IF condition2. " ... " ENDIF.ELSE. " ... "ENDIF.Examples
1. Simple IF
DATA quantity TYPE i VALUE 15.
IF quantity > 10. WRITE / 'Order quantity is greater than 10.'.ENDIF.2. IF with ELSE
DATA user_input TYPE c LENGTH 1.user_input = 'X'. " Example input
IF user_input = 'Y'. WRITE / 'User said Yes.'.ELSE. WRITE / 'User did not say Yes.'.ENDIF.3. IF with ELSEIF and ELSE
DATA score TYPE i VALUE 75.DATA grade TYPE c LENGTH 1.
IF score >= 90. grade = 'A'.ELSEIF score >= 70. grade = 'B'.ELSEIF score >= 50. grade = 'C'.ELSE. grade = 'D'.ENDIF.
WRITE / 'The grade is:', grade. " Output: The grade is: B4. Combined Condition (AND)
DATA has_permission TYPE abap_bool VALUE abap_true.DATA is_logged_in TYPE abap_bool VALUE abap_true.
IF has_permission = abap_true AND is_logged_in = abap_true. WRITE / 'Access granted.'.ELSE. WRITE / 'Access denied.'.ENDIF.Distinction from CASE
When you want to check the same variable against multiple different, fixed values, the CASE statement is often clearer than a long chain of IF ... ELSEIF statements:
DATA transport TYPE c LENGTH 1. " B=Bike, C=Car, T=Train
transport = 'C'.
" With IF/ELSEIFIF transport = 'B'. WRITE / 'Bike selected.'.ELSEIF transport = 'C'. WRITE / 'Car selected.'.ELSEIF transport = 'T'. WRITE / 'Train selected.'.ELSE. WRITE / 'Unknown.'.ENDIF.
" With CASE (often more readable)CASE transport. WHEN 'B'. WRITE / 'Bike selected.'. WHEN 'C'. WRITE / 'Car selected.'. WHEN 'T'. WRITE / 'Train selected.'. WHEN OTHERS. WRITE / 'Unknown.'.ENDCASE.Important Notes / Best Practice
- The
IFstatement is fundamental to almost every ABAP program. - Pay attention to clear and unambiguous logical expressions.
- Use indentation consistently to make the structure readable.
- Never forget the closing
ENDIF. - For purely value-based assignments, modern ABAP (>= 7.40) often offers more compact alternatives like the conditional operators
CONDandSWITCH. However,IFremains essential for controlling statement blocks.