Skip to Content
Technical Articles

Ternary operator in ABAP

After coming into contact with ABAP, developers with experience in other programming languages often wonder if there is a ternary operator available in ABAP similar to the construct a ? b : c.

Unfortunately, there is not.

However, using 7.40 syntax, we can achieve something that comes pretty close with the conditional operator COND:

DATA(is_even) = COND #(
  WHEN number MOD 2 EQ 0
  THEN abap_true
  ELSE abap_false ).

The code above is equivalent to the more conventional if-else statement below:

IF number MOD 2 EQ 0.
  DATA(is_even) = abap_true.
ELSE.
  is_even = abap_false.
ENDIF.

Finally, note that for this particular example, filling a boolean variable based on a condition, the more concise xsdbool function can be used:

DATA(is_even) = xsdbool( number MOD 2 EQ 0 ).
Be the first to leave a comment
You must be Logged on to comment or reply to a post.