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

COND - Conditional Operator

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:

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.

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( ) ) ).