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 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. This type is then used. In particular, THROWs cannot be specified after THEN.

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.

In the parentheses, WHEN must be specified with at least once with any logical expression log_exp. 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 of 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.

A LET expression can be specified before the first WHEN, after every THEN and after ELSE to define local auxiliary fields.


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, which makes it 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( ) ) ).