Summary: in this tutorial, you will learn how to use the ABAP if else statement to execute a block of code based on the result of one or more logical expressions.
The if statement
To execute a block of code based on a logical expression, you use the if statement as follows:
if logical_expression.
" code block
endif.In this syntax, the code block inside the if statement will execute if the logical_expression evaluates to true.
If the logical_expression evaluates to false, the control is passed to the next statement in the program.
For example:
data gv_status type i value 1.
if gv_status = 1.
write: 'The status is 1'.
endif.Output:
The status is 1In this example, the value of the gv_status is 1. The if statement checks if the value of gv_status is one and outputs a message to the screen.
If you change the value of gv_status to 2, the expression gv_status = 1 evaluates to a false, the code does not output anything to the screen.
data gv_status type i value 2.
if gv_status = 1.
write: 'The status is 1'.
endif.To execute another block of code in case the condition in the if statement evaluates false, you use the if else statement.
The if else statement
The following shows the syntax of the if else statement:
if logical_expression.
" if block
else.
" else block
endif.In this syntax, if the logical_expression evaluates to true, the code in the if branch will execute.
In case the logical_expression evaluates to false, the code block in the else branch will execute.
For example:
data gv_status type i value 2.
if gv_status = 1.
write: 'The status is 1'.
else.
write: 'The status is not equal to 1'.
endif.Output:
The status is not equal to 1The if elseif else statement
The if elseif else statement allows you to execute a code block based on the results of multiple boolean expressions:
if logical_expression1.
" if block
elseif logical_expression2.
" elseif block
elseif logical_expression3.
"...
else.
" else block
endif.In this syntax, ABAP evaluates the logical expressions from top to bottom. If it finds a logical expression that evaluates to true, it will execute the code block in that branch.
If there is no logical expression that evaluates to true, the code block in the else branch will execute.
Consider the following example:
data: gv_quantity type i value 120,
gv_price type i.
if gv_quantity > 0 and gv_quantity <= 10.
gv_price = 20.
elseif gv_quantity > 10 and gv_quantity <= 50.
gv_price = 18.
elseif gv_quantity > 50 and gv_quantity <= 100.
gv_price = 16.
else.
gv_price = 15.
endif.
write gv_price.Output:
15How it works.
- First, declare two variables
gv_quantityandgv_price. The default value of thegv_quantityis 120. - Second, use the
if esleif elsestatement to determine the value for thegv_pricevariable based on the value of thegv_quantity.variable. Sincegv_priceis 120, the code block in theelsebranch is executed, which assignsgv_pricethe value 15. - Finally, output the value of the
gv_pricevariable to the screen.
Summary
- Use the
ifstatement when you want to execute a code block based on a logical expression. - Use the
if elsestatement in case you want to execute another code block when the logical expression in theifbranch isfalse. - Use the
if elseif elsestatement when you want to execute a code block based on the results of multiple logical expressions.