ABAP IF ELSE and Case: ABAP Control Statements

ABAP IF ELSE and Case statements also known as ABAP Control Statements are used to control the flow of the ABAP Program based on Logical statements.

We have four types of Control Statements:

IF Statements

The code between Keyword IF and END IF is executed only if the condition is true.

Example:

DATA:   LV_NUMBER TYPE i VALUE 32.

IF LV_NUMBER > 30.

WRITE: / ‘You have passed the examination’.

ENDIF.

Output:

You have passed the examination

IF-ELSE Statements

The code between Keyword IF and ELSE is executed only if the condition is true and the code between Keyword ELSE and END IF is executed only if the condition is false. It is used to check two conditions simultaneously.

Example:

DATA:   LV_NUMBER TYPE i VALUE 29.

IF LV_NUMBER > 30.

WRITE: / ‘You have passed the examination’.

ELSE.

WRITE: / ‘You have failed the examination’.

ENDIF.

Output:

You have failed the examination

 

IF-ELSEIF Statements

It is used to validate multiple conditions one by one. The syntax is just like above, apart from that, it includes Keyword ELSE IF.

Example:

DATA:   LV_NUMBER TYPE i VALUE 31.

IF LV_NUMBER >90.

WRITE: / ‘You have passed the examination with Distinction’.

ELSEIF LV_NUMBER >30 AND LV_NUMBER <90.

WRITE: / ‘You have passed the examination’.

ELSE.

WRITE: / ‘You have failed the examination’.

ENDIF.

Output:

You have passed the examination

 

CASE-ENDCASE Statements

It is used to validate conditions of a single variable based on its content. The Keywords are CASE , WHEN and END CASE. Let us suppose, you are making a calculator where you provide +, -, x, / option. For that you can take single input and on the basis of input you can perform action.

Example:

DATA:   LV_INPUT1         TYPE i,

LV_INPUT2         TYPE i,

LV_OPERATION                TYPE c,

LV_OUTPUT                       TYPE i.

CASE LV_OPERATION.

WHEN ‘+’.

WRITE: / ‘The Sum is:’ LV_INPUT1 + LV_INPUT2.

WHEN ‘*’.

WRITE: / ‘The Product is:’ LV_INPUT1 * LV_INPUT2.

WHEN OTHERS.

WRITE: / ‘Not Found’.

ENDCASE.

ABAP control statments are seldom compared with ABAP Loop Control statements, which are different and mentioned below:

Loop Control Statements

If you want to break the normal flow of an ABAP loop, then you need loop control statements. ABAP provide three loop control statements i.e. CONTINUE, CHECK, EXIT.

CONTINUE

This statement will pass the current loop unconditionally. It needs IF statements to apply condition. As soon this statement is read by the compiler, it skips the current loop and goes to next iteration, this will be clearer with an example.

CHECK

This statement will pass the current loop conditionally. If the CHECK condition is true then it executes the rest of the statements otherwise it skips the current loop and goes to next iteration, this will be clearer with an example.

EXIT

This statement will end all the iterations as soon this statement is read by the compiler. It also needs IF statements to apply condition. After EXIT statement the program control goes to the statement just after the LOOP statements.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Translate »