Scroll Top

New Features in ABAP 7.4 – Conditional Logic

ABAPAnthony Cecchini is the President of Information Technology Partners (ITP), an SAP consulting company headquartered in Pennsylvania. ITP offers comprehensive planning, resource allocation, implementation, upgrade, and training assistance to companies. Anthony has over 20 years of experience in SAP business process analysis and SAP systems integration. His areas of expertise include SAP NetWeaver integration; ALE development; RFC, BAPI, IDoc, Dialog, and Web Dynpro development; and customized Workflow development. You can reach him at [email protected].

Conditional Logic in ABAP 7.4

The continuing theme in all of these posts, is how we can make our code thinner, more readable and transparent. Well, nothing clogs up the visual readability of your code like long IF-THEN-ELSE-ENDIF or CASE-WHEN-ENDCASE constructs. Well, in ABAP 7.4 we have some new Constructor Operators we can use in Constructor Expressions that will make our code easier to read, more compact, and safer to maintain.

You remember Constructor Operators… We discussed them in the last blog post New Features in ABAP 7.4 – Calling Methods and Functions. In That post we highlighted the CONV Constructor Operator and how we could use it to convert a local variable to a STRING. In this post, I’d like to talk about the Constructor Operators COND and SWITCH. Again, for a complete list see the SAP ABAP 7.4 help for more information.

Using COND as a Replacement for IF/ELSE in ABAP 7.4

Since CASE statements can only evaluate one variable at a time, we use the IF/ELSE construct when we need to check multiple conditions. Look at the example below…

DATA: lv_text(30).

IF lv_vehicle = '01' AND lv_type = 'C'.
    lv_text = 'Toyota'.
IF lv_vehicle ='02' AND lv_type = 'C'.
   lv_text = 'Chevy'
IF lv_vehicle ='03' AND lv_type = 'C'.
   lv_text = 'Range Rover'.


In ABAP 7.4, you can achieve the same thing, but you can do this in a more economical way by using the COND constructor operator. This also means that you do not have to keep specifying the target variable again and again. We will also add an ABAP INLINE Declaration… by using the DATA statement to create the variable lv_text inline on the fly!

DATA(lv_text) = COND text30(
     WHEN lv_vehicle ='01' AND lv_type = 'C' THEN 'Toyota'
     WHEN lv_vehicle ='02' AND lv_type = 'C' THEN 'Chevy'
     WHEN lv_vehicle ='03' AND lv_type = 'C' THEN 'Range Rover').

Using SWITCH a Replacement for CASE in ABAP 7.4

Here, we’re getting the day of the week and using a CASE statement to turn the number into a string, such as “Monday”, to output at the top of a report.

data: l_indicator like scal-indicator,
      l_day(10) type c.

call function 'DATE_COMPUTE_DAY'
        date = p_date
        day = l_indicator.

case l_indicator.
    when 1.
      l_day = 'Monday'.
    when 2.
      l_day = 'Tuesday'.
    when 3.
      l_day = 'Wednesday'.
    when 4.
      l_day = 'Thursday'.
    when 5.
      l_day = 'Friday'.
    when 6.
      l_day = 'Saturday'.
    when 7.
      l_day = 'Sunday'.
     Raise exception type zcx_day_problem.

With ABAP 7.4, we can simplify and accomplish the same thing by by using the new SWITCH constructor operator instead of the CASE sttatement. Like so…

DATA(L_DAY) = SWITCH char10( l_indicator
   when 1 THEN 'Monday'
   when 2 THEN 'Tuesday'
   when 3 THEN 'Wednesday'
   when 4 THEN 'Thursday'
   when 5 THEN 'Friday'
   when 6 THEN 'Saturday'
   when 7 THEN 'Sunday'
      ELSE THROW zcx_day_problem( ) ).

Also note that if you are tired of only catching exceptions you can throw exceptions now! The usage is identical to the RAISE EXCEPTION TYPE, however, the compiler evaluates the keywords RAISE EXCEPTION TYPE and THROW as if they were one and the same.

Using Predictive Method Calls in ABAP 7.4

For quite some time the lack of a real Boolean type in ABAP led to a lot of discussions amongst developers. I would see post on the SCN community constantly asking why a developer couldn’t write..

IF meth( … ).

The answer from SAP was always “Because you have to have a Boolean type behind IF and no method can return a Boolean type.” But, now in Release 7.40, SP08 You can write predicative method calls as shown below..

… meth( ) …

as a short form of the relational expression

… meth( ) IS NOT INITIAL …

So you can place simple functional method calls everywhere, where logical expressions are allowed: behind IF, CHECK, ASSERT, COND, SWITCH, … Here is an example…

IF zcl_system=>is_production( ).
"In production we never want a short dump
      zcl_application=>main( ).
      CATCH cx_sy_no_handler INTO gcl_no_handler.

The reason this works is spelled out completely in Paul Hardy’s Book ABAP to The Future. Paul explains, “What’s happening from a technical point of view is that if you don’t specify anything after a functional method the compiler evaluates it as IS_PRODUCTION( ) IS NOT INITIAL. An ABAP_TRUE value is really the letter X, so the result is not initial, and so the statement is resolved as true.”

To learn more about Predictive Methods and Predicate methods, take a look at this Documentation and then check out Horst Keller’s Blog.

Using the New Boolean Function XSDBOOL in ABAP 7.4

Another common situation with respect to Boolean logic in ABAP, is when you want to send a TRUE/FALSE value to a method or get such a value back from a functional method. For instance, did you ever stumble over this one?

IF boolc( 1 = 2 ) = ABAP_FALSE.
  WRITE: / 'YES'.
  WRITE: / 'NO'.

Guess what? The relational expression “boolc( 1 = 2 ) = ABAP_FALSE” is false! Why? Because BOOLC despite its name does not return c but a string and the comparison rules for c and string blah, blah, blah …

Technically BOOLC( 1 = 2) evaluates to a string containing a blank of length 1. So far so good. But comparing ` ` with ‘ ‘ or ABAP_FALSE is false, since the text field is converted to string resulting in an empty string.

Now in ABAP 7.4, a new built-in function was added, called XSDBOOL, which does the same thing as BOOLC but returns an ABAP_BOOL type parameter.

IF xsdbool( 1 = 2 ) = ABAP_FALSE.
  WRITE: / 'YES'.
  WRITE: / 'NO'.

The relational expression xsdbool( 1 = 2 ) = abap_false is true, because xsdbool returns type XSDBOOLEAN from the ABAP Dictionary that is – yes, you guess it – c of length 1. For the experts among you, XSDBOOLEAN is normally used for special mappings in XML-transformations and was reused here for quite another purpose. And that’s were the funny name xsdbool comes from.

Again, I highly recommend reading Horst Keller’s Blog on all the ABAP 7.4 enchantments to the code base.


With the new conditional code constructs in ABAP 7.2 and ABAP 7.4 we have the ability to create ABAP code with fewer statements for the same functionality, without compromising readability of the code. New boolean functions like XSDBOOL, and new conditional operators such as SWITCH and COND allow us more flexability and extensibility when developing and maintaining our ABAP code.

ITP logo

If you enjoyed this blog, New Features in ABAP 7.4 – Conditional Logic, please fill out the form below to sign up for our newsletter. We deliver SAP Technical tips & tricks, SAP news, and the current month’s BLOG right to your inbox!

Related Posts

Related Posts

Pin It on Pinterest

Share This

If you enjoyed this post, why not share it with your friends!