SAP ABAP Tutorial for Beginner – Previously we have learned how to do calculation in ABAP using Arithmetic and Math Functions, Now we will learn ABAP Flow Control, this article cover how to add logic to our ABAP Programming. As another programming, we also know logic operation such as : IF .. ELSE , WHILE, etc.
1.IF Statements
An IF statement evaluates whether a certain condition is true or false. If that condition is true, the code within the statement will execute; otherwise, it will continue.
Following table contain lists of condition operator in ABAP .
Opoerators | Description |
---|---|
!ERROR! undefined variable 'oryoucanuseeq' | Equal: true if both values are equal to each other |
< > or NE | Not equal: true if both values do not equal each other |
< or LT | Less than: true if the number on the left is less than the number on the right |
> or GT | Greater than: true if the number on the left is greater than the number on the right |
<= or LE | Less equal: true if the number on the left is less than or equal to the number on the right |
>= or GE | Greater equal: true if the number on the left is greater than or equal to the number on right |
BETWEEN n and n1 | Between: true if the number on the left is between n and n1 |
For Example :
1 2 3 4 5 6 7 |
DATA : lv_test TYPE i . lv_test = 1. IF lv_test = 1. WRITE : 'Your Value is TRUE' . ENDIF. |
As long as the value of lv_test is 1, you’ll see “Your Value is TRUE” printed on the screen.
You also can combine multiple operators using the AND and OR key- AND/OR IF words. For AND, both operators must return true before the code statements within the IF statement will run.
For example we will use IF .. AND :
1 2 3 4 5 6 7 8 9 |
DATA : lv_test TYPE i, lv_test2 type i. lv_test = 1. lv_test2 = 2. IF lv_test = 1 AND lv_test2 = 1. WRITE : 'Your Value is TRUE' . ENDIF. |
“Your Values is TRUE” will not be written in screen because lv_test and lv_test2 not equal 2.
IF Parenthesis for complex IF Statements
Just as with the arithmetic calculations, we can use parentheses to create more complex IF statements. Parentheses group IF statement tests together; those IF statements can be combined with an AND or OR, as in the following example.
1 2 3 4 5 6 7 8 |
DATA : lv_test TYPE i . lv_test = 1. IF ( lv_test = 0 AND lv_test = 1 ) OR ( lv_test = 0 OR lv_test = 1 ). WRITE : 'Hello'. END IF . |
IF .. ELSE Statements
When the initial condition is evaluated as false, you can add the ELSE keyword, which will run the code within the ELSE and ENDIF keywords when the test for IF is false.
For Example : ‘ELSE’ would be written to the screen, because the value of d_test is not between 5 and 10, causing the code under the ELSE to be executed.
1 2 3 4 5 6 7 8 9 10 |
DATA : lv_test TYPE i . lv_test = 1. IF lv_test BETWEEN 5 AND 10. HRITE : 'Hello'. ELSE. WRITE: 'ELSE'. ENDIF. |
IF .. ELSEIF Statements
You may want to evaluate multiple conditions before utilizing the ELSE statement, which is a case in which the ELSE IF keyword can be useful. ELSE IF will only evaluate its condition if the IF /ELSE IF before it was evaluated as false.
For example : ELSE IF lv_test = 1 was true, it wasn’t evaluated because the test before it was found to
be true, so ‘ELSEIF’ was written to the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
DATA : lv_test TYPE i . lv_test ~ 1. IF lv_test BETWEEN 5 AND 10 WRITE: 'IF'. ELSEIF lv_test > 0. WRITE: 'ELSEIF' . ELSEIF d_test = 1. WRITE: 'ELSEIF 1'. ELSE. WRITE : 'ELSE'. ENDIF. |
2.CASE Statements
A CASE statement works like an IF statement, except that the CASE CASE statement will always evaluate only one variable and only uses the equal to condition. The CASE statement uses the keyword WHEN to evaluate
the variable against a value and WHEN OTHERS to handle any value that was not caught by the existing tests. The example shown below will write ‘1’ to the screen and not execute any of the other WRITE commands.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
DATA : lv_test TYPE i . lv_test = 1. CASE lv_test. WHEN 0. WRITE: '0'. WHEN 1. WRITE: '1'. WHEN 2. WRITE: '2'. WHEN OTHERS. WRITE: 'Others'. ENDCASE. |
3.DO Loops
Using the keyword DO, you can execute a block of code any number of times. Define the number of times by adding a number and the keyword TlMES. The block of code that will run is between DO and ENDDO. In the following example, the code within the loop will be executed 5 times and the number 5 will be written to the screen.
1 2 3 4 5 6 7 |
DATA : lv_test TYPE i VALUE 0. DO 5 TIMES. lv_test = lv_test + 1. ENDDO. WRITE : lv_test . |
4.WHILE Loops
A WHILE loop works like a DO loop, but instead of indicating a number of times that the loop content is to be run, it indicates a condition that will cause the loop to stop. Any operation condition that is valid in an IF statement will be valid for the WHILE loop.
In the example below, the code within the loop will be executed until the value of lv_test is no longer less than 5. As a result, the number 5 will be written to the screen since 5 is not less than 5.
1 2 3 4 5 6 7 |
DATA : lv_test type i VALUE 0. WHILE lv_test < 5. lv_test = lv_test + 1. ENDWHILE . WRITE : lv_test . |
Next lesson we will learn how to create Classic selection screen programming as interface with user.