ABAP CASE Statement: Multiple Branches for Clear Code

Category
ABAP-Statements
Published
Author
Johannes

The CASE statement is a control structure for multiple branches in ABAP. It often provides a clearer alternative to a long chain of IF...ELSEIF...ELSE...ENDIF statements, especially when the program flow depends on which of several specific, discrete values a single variable (or expression) has.

You check the value of one operand and then execute the code block that matches that value.

Syntax

CASE <operand>.
WHEN <value1> [OR <value1a> OR <value1b> ...].
" Statement block 1
" Executed when <operand> = <value1> OR <value1a> OR <value1b> ...
[Statements...]
WHEN <value2> [OR <value2a> ...].
" Statement block 2
" Executed when <operand> = <value2> OR <value2a> ...
" AND no previous WHEN condition matched.
[Statements...]
" ... additional WHEN branches ...
[WHEN OTHERS. " Optional branch for all other cases
" Statement block 'Others'
" Executed when <operand> matches NONE of the values in the
" previous WHEN branches.
[Statements...]]
ENDCASE.
  • CASE <operand>: Introduces the structure. <operand> is the variable or expression whose value is checked.
  • WHEN <value>: Defines a possible case (a branch). <value> is a constant value (e.g., a literal like 'A', 123 or a constant) against which the <operand> is compared.
  • OR <valueX>: Optional. Allows specifying multiple values for which the same statement block should be executed.
  • Statement block: The block of ABAP code that is executed when the value of <operand> matches one of the values in the WHEN branch.
  • WHEN OTHERS: An optional branch that catches all values not covered by any of the previous WHEN branches. It’s good practice to often include this branch to handle unexpected values.
  • ENDCASE: Mandatory closing of the CASE structure.

Behavior and Flow

  1. The value of <operand> is determined once.
  2. This value is compared sequentially (from top to bottom) with the values in the WHEN branches.
  3. As soon as the operand’s value matches a value in a WHEN branch, the corresponding statement block is executed.
  4. Important: After executing this block, control jumps immediately to the statement after ENDCASE. No further WHEN branches within the same CASE structure are checked or executed – “first matching WHEN wins”.
  5. If none of the WHEN branches match, the WHEN OTHERS block is executed (if present).
  6. If none of the WHEN branches match and there is no WHEN OTHERS block, none of the blocks inside the CASE structure are executed, and the program continues after ENDCASE.

Comparison with IF...ELSEIF...

  • CASE: Ideal when checking one value against multiple fixed individual values. Often more readable and shorter.
  • IF...ELSEIF...: More flexible for complex conditions, range checks (>, <), combinations (AND, OR), or when different variables need to be checked in the conditions.

Modern Alternative: The SWITCH Operator (from ABAP 7.40)

For assignments based on the value of a variable, there’s the SWITCH operator that offers more compact inline syntax:

DATA(description) = SWITCH additional_type( transport_mode
WHEN 'B' THEN 'Bicycle'
WHEN 'C' THEN 'Car'
WHEN 'U' THEN 'Bus'
ELSE 'Unknown' ).

However, CASE remains the standard statement for controlling statement blocks.

Examples

1. CASE for a Status Code (Character)

DATA document_status TYPE c LENGTH 1.
document_status = 'R'. " R=Released, B=Blocked, N=New
CASE document_status.
WHEN 'N'.
WRITE / 'Document is new and not yet processed.'.
WHEN 'R'.
WRITE / 'Document is released.'. " This block is executed
WHEN 'B'.
WRITE / 'Document is blocked.'.
WHEN OTHERS.
WRITE / 'Warning: Unknown document status:', document_status.
ENDCASE.

2. CASE for Month Numbers with OR

DATA current_month TYPE i.
current_month = sy-datum+4(2). " Extracts month from system date
CASE current_month.
WHEN 1 OR 2 OR 3.
WRITE / 'First quarter'.
WHEN 4 OR 5 OR 6.
WRITE / 'Second quarter'. " This block is executed in April
WHEN 7 OR 8 OR 9.
WRITE / 'Third quarter'.
WHEN 10 OR 11 OR 12.
WRITE / 'Fourth quarter'.
WHEN OTHERS.
WRITE / 'Invalid month extracted!'. " Should not happen
ENDCASE.

3. CASE for User Input

DATA user_command TYPE sy-ucomm. " System field for OK codes
user_command = 'SAVE'.
CASE user_command.
WHEN 'SAVE'.
WRITE / 'Action: Executing save.'.
" ... Code for saving ...
WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
WRITE / 'Action: Back or Cancel.'.
LEAVE TO SCREEN 0. " Example action
WHEN OTHERS.
WRITE / 'Action: Unknown or unhandled command.'.
ENDCASE.

4. CASE without WHEN OTHERS

DATA flag TYPE c LENGTH 1 VALUE 'X'.
CASE flag.
WHEN 'A'.
WRITE / 'Flag is A'.
WHEN 'B'.
WRITE / 'Flag is B'.
ENDCASE.
" Since flag = 'X' and no WHEN OTHERS exists, no block is executed.
WRITE / 'After the CASE block.'.

Important Notes / Best Practice

  • CASE is very useful for improving readability when comparing one operand with many fixed values.
  • Use the WHEN OTHERS branch to ensure your program responds robustly to unexpected or not explicitly handled values.
  • Remember the “First Match Wins” logic: Only the first matching WHEN branch is executed.
  • Don’t forget the closing ENDCASE.