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:

abap
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:

abap
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:

abap
DATA(is_even) = xsdbool( number MOD 2 EQ 0 ).
Discuss this post on Community
Laurens Deprost
Laurens Deprost
SAP Consultant

Related