Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Program Flow →  Control Structures →  Branches 

CASE

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


CASE operand. 
  [WHEN operand1 [OR operand2 [OR operand3 [...]]].
    [statement_block1]]
  ...
  [WHEN OTHERS.
    [statement_blockn]]
ENDCASE.

Effect

Case Distinction These statements define a control structure that can contain multiple statement blocks statement_block1, ..., statement_blockn, of which no more than one is executed depending of the value in the operand operand.

Starting with the first WHEN statement, the content of the operand in operand is compared with the content of one of the operands operand1, operand2, ... from the top down. The statement block is executed after the first identical instance is found. If no matches are found, the statement block is executed after the statement WHEN OTHERS.

operand, operand1, operand2, ... are enhanced functional operand positions alongside which all functional methods and all predefined functions which have exactly one unnamed argument can be specified.

If the end of the executed statement block is reached or no statement block is executed, processing continues after ENDCASE.

The contents are compared as illustrated in the following logical expression:

operand = operand1 [OR operand = operand2
                   [OR operand = operand3 [...]]]

For the comparison, the comparison rules for comparisons between any operands apply, depending on the data types of the operands involved.


Notes

  • A statement cannot be placed between the statement CASE and the first statement WHEN. In classes, this produces a syntax error; outside classes, obsolete syntax of this type produces a syntax warning.
  • For operand, the current value is used in every comparison. This may differ from the starting value if operand is a variable that is changed in a functional method specified after a WHEN statement.
  • A CASE control structure is somewhat faster than a semantically equivalent IF control structure.

Example

Branches the program flow depending on the function code in system field sy-ucomm.

CASE sy-ucomm. 
  WHEN 'BACK'. 
    LEAVE TO SCREEN 100. 
  WHEN 'CANCEL'. 
    LEAVE SCREEN. 
  WHEN 'EXIT'. 
    LEAVE PROGRAM. 
  WHEN OTHERS. 
    MESSAGE '...' TYPE 'E'. 
ENDCASE. 

Continue

WHEN

ENDCASE