Skip to content

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

COND - Conditional Operator

Other versions: 7.31 | 7.40 | 7.54

Syntax


... COND type( [let_exp] 
               WHEN log_exp1 THEN [ let_exp] result1
             [ WHEN log_exp2 THEN [ let_exp] result2 ]
             ...
             [ ELSE [ let_exp] resultn ] ) ...

Effect

A conditional expression with the conditional operator COND has a result, result, that is specified by logical expressions. 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.

WHEN must be specified at least once with any logical expression log_exp in the parentheses. This can be followed by any number of WHENs with further logical expressions. An ELSE can be specified at the end. The expression evaluates the logical expressions one after the other and selects the result specified (after THEN) in the first logical expression whose result is true. The selected result determines the result of the conditional expression. If none of the logical expressions are true, the result specified after ELSE is selected. If ELSE is not specified, the result is the initial value of the data type type.

To define local helper fields, an optional LET expression can be specified before the first WHEN, after every THEN, and after ELSE.


Notes

  • 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

Transforms a time to 12 hour format using a conditional expression in an operand position. The type of the result is used by the operand after the first specified THEN. This makes the type string.

CLASS cx_cant_be DEFINITION INHERITING FROM cx_no_check. 
ENDCLASS. 

cl_demo_output=>display( 
  COND #( LET t = '120000' IN 
          WHEN sy-timlo < t THEN 
            |{ sy-timlo TIME = ISO } AM| 
          WHEN sy-timlo > t AND sy-timlo < '240000' THEN 
            |{ CONV t( sy-timlo - 12 * 3600 ) TIME = ISO } PM| 
          WHEN sy-timlo = t THEN 
            |High Noon| 
          ELSE 
            THROW cx_cant_be( ) ) ).