Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Program Flow Logic →  Conditional Expressions 

SWITCH - Conditional Operator

Other versions: 7.31 | 7.40 | 7.54

Syntax


... SWITCH type( [let_exp] 
                 operand
                 WHEN const1 THEN [ let_exp] result1
               [ WHEN const2 THEN [ let_exp] result2 ]
               ...
               [ ELSE [ let_exp] resultn ] ) ...

Effect

A conditional expression with the conditional operator SWITCH has a result, result, that is specified by a case distinction. Either a value with the data type specified by type is produced or a class-based exception is raised. The following can be specified for type:

  • A non-generic data type dtype.
  • If the data type required in an operand position is unique and fully identifiable, this type is used.
  • If the operand type is not fully identifiable, an operand with a statically identifiable type must be specified after the first THEN (except when passing the constructor parameter to an actual parameter with generically typed formal parameter). This type is then used. In particular, THROWs cannot then be specified after THEN.
  • If the constructor expression is passed to an actual parameter with generically typed formal parameter, the operand type is derived in accordance with special rules.
  • In other cases, the character # cannot be specified.

All operands specified after THEN must be convertible to the data type determined by type. In the case of reference variables, an up cast must be possible.

The position operand in the parentheses is the value checked in the case distinction. This is a general expression position. It must be followed by at least one WHEN. Literals and constants can be specified for const after WHEN. It must be possible to compare them with operand. Substring access is not possible. This can be followed by any number of WHENs with further constant values. An ELSE can be specified at the end. This expression compares the values of the operand operand with the specified constant values, one by one, and chooses the result after THEN for which the values of operand and constant are identical for the first time. The selected result determines the result of the conditional expression. If no matches are found, the result specified after ELSE is selected. If ELSE is not specified, the result is the initial value of the data type type.

If an item specified after THEN or ELSE can be selected, either the result is set or a class-based exception is raised, just as with a conditional expression COND.

To define local helper fields, an optional LET expression can be specified in front of the operand operand, after every THEN, and after ELSE.


Notes

  • A conditional expression with SWITCH has the same meaning as the following conditional expression with COND:
COND type( WHEN operand = const1 THEN result1
         [ WHEN operand = const2 THEN result2 ]
         ...
         [ ELSE resultn ] )

  • Rules apply when deriving the type in cases where # is specified for actual parameters that can be passed to generically typed formal parameters. These rules prevent syntax errors in programs that call a procedure and the procedure makes the full typing of a formal parameter type more general by switching to a generic type.

  • No suitable full type can be derived for formal parameters with the generic types c, n, and x.

Example

Conditional operator SWITCH in an operand position in a loop. The loop is exited when the exception after ELSE is caught.

CLASS cx_overflow DEFINITION INHERITING FROM cx_static_check. 
ENDCLASS. 

DATA(out) = cl_demo_output=>new( ). 
DO. 
  TRY. 
      out->write( 
        SWITCH string( sy-index 
                       WHEN 1 THEN 'one' 
                       WHEN 2 THEN 'two' 
                       WHEN 3 THEN 'three' 
                       ELSE THROW cx_overflow( ) ) ). 
    CATCH cx_overflow. 
      out->display( ). 
      EXIT. 
  ENDTRY. 
ENDDO.