Select Page

# ABAP IF ELSE and Case

by | Mar 8, 2018 | ABAP Beginner

# 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. 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 validates 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 the 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, / options. For that, you can take a single input, and on the basis of input, you can perform an 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.
ENDCASE.```

ABAP control statements are seldom compared with ABAP Loop Control statements. They 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 conditions. As soon as the compiler reads this statement, it skips the current loop and goes to the next iteration. This will be clearer with an example.

### CHECK

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

### EXIT

This statement will end all the iterations as soon as the compiler reads this statement. It also needs IF statements to apply conditions. After the EXIT statement, the program control goes to the statement just after the LOOP statements.