9

Ternary operator in ABAP

 2 years ago
source link: https://blogs.sap.com/2021/07/17/ternary-operator-in-abap/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Technical Articles
Posted on July 17, 2021 Less than a 1 minute read

Ternary operator in ABAP

2 Likes 31 Views 0 Comments

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK