Branching Conditionally 

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

This control structure is introduced with the IF statement. The IF statement allows you to divert the program flow to a particular statement block, depending on a condition. The statement block concludes either with ENDIF, ELSEIF, or ELSE.

IF <condition1>.
  <statement block>
ELSEIF <condition2>
  <statement block>.
ELSEIF <condition3>.
  <statement block>
.....
ELSE.
  <statement block>
ENDIF.

To formulate conditions in IF or ELSEIF statements, you can use any logical expression.

If the first condition is true, the system executes all the statements up to the end of the first statement block and then continues processing after the ENDIF statement. If the first condition is not true, the program jumps to the next ELSEIF statement and executes it like an IF statement. ELSE begins a statement block which is processed if none of the IF or ELSEIF conditions is true. The end of the last statement block must always be concluded by ENDIF.

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.

DATA: TEXT1(30) VALUE 'This is the first text',
TEXT2(30) VALUE 'This is the second text',
TEXT3(30) VALUE 'This is the third text',
STRING(5) 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 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

This control structure is introduced with the CASE statement. The CASE control structure allows you to control which statement blocks are processed based on the contents of a data object.

CASE <f>.
  WHEN <f11> [OR <f 12> OR ...].
       <Statement block>
  WHEN <f21>.[OR <f 22> OR ...]
       <Statement block>
  WHEN <f31> [OR <f 32> OR ...].
       <statement block>
WHEN ...
  ......
  WHEN OTHERS.
       <statement block>
ENDCASE.

The statement block following a WHEN statement is executed if the contents of <f> are the same as those of one of the fields <f ij >. Afterwards, the program carries on processing after the ENDCASE statement. The statement block after the optional WHEN OTHERS statement is executed if the contents of <f> does not equal any of the <f ij > contents. The last statement block must be concluded with ENDCASE.

The CASE control structure is a shortened form of the following IF structure:

IF <f> = <f11> OR <f> = <f 12> OR <f> = ...
  <Statement block>
ELSEIF <f> = <f21> OR <f> = <f 22> OR <f> =...
  <Statement block>
ELSEIF <f> = <f21> OR <f> = <f 22> OR <f> =...
  <statement block>
ELSEIF <f> = ...
  ...
ELSE.
  <statement block>
ENDIF.

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

DATA: TEXT1 VALUE 'X',
TEXT2 VALUE 'Y',
TEXT3 VALUE 'Z',
STRING 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 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’.