Show TOC

Branching ConditionallyLocate this document in the navigation structure

When you branch conditionally, a processing block is executed or not based on the result of one or more logical conditions. ABAP contains two control structures for conditional branching.

The IF Control Structure

You can nest IF control structures.

 However, the statement blocks must all end within the current processing block. So, for example, an IF - ENDIF block may not contain an event keyword.

Tip

REPORT demo_flow_control_if.

DATA: text1(30) TYPE c VALUE 'This is the first text',      text2(30) TYPE c VALUE 'This is the second text',      text3(30) TYPE c VALUE 'This is the third text',      string(5) TYPE c VALUE 'eco'.

IF text1 CS string.  WRITE / 'Condition 1 is fulfilled'.ELSEIF text2 CS string.  WRITE / 'Condition 2 is fulfilled'.ELSEIF text3 CS string.  WRITE / 'Condition 3 is fulfilled'.ELSE.  WRITE / 'No condition is fulfilled'.ENDIF.

The list output is:

Condition 2 is fulfilled.

Here, the second logical expression text2 CS string is true because the string "eco" occurs in text2.

The CASE Control Structure

You can nest CASE control structures and also combine them with If control structures. However, they must always end with an ENDCASE statement within the current processing block.

Tip

REPORT demo_flow_control_case.

DATA: text1   TYPE c VALUE 'X',      text2   TYPE c VALUE 'Y',      text3   TYPE c VALUE 'Z',      string  TYPE c VALUE 'A'.

CASE string.  WHEN text1 OR text2.    WRITE: / 'String is', text1, 'OR', text2.  WHEN text3.    WRITE: / 'String is', text3.  WHEN OTHERS.    WRITE: / 'String is not', text1, text2, text3.ENDCASE.

The list output is:

String is not X Y Z

Here, the last statement block after WHEN OTHERS is processed because the contents of string 'A' does not equal 'X', 'Y', or 'Z'.