ABAP 740 SWITCH – Conditional Operator

By | October 25, 2015 | Concepts | 58,889 | 1

SWITCH is a conditional operator introduced in ABAP 740 similar to CASE but powerful and with much less coding. Who doesn’t like that? Lets check it out.

Introduction

Conditional operator is like you have IF .. ELSEIF .. ELSE .. ENDIF. Many times you try to avoid that and use the CASE statement. Since 740, we have a new operator added, SWITCH. As name suggests, it should be used to switch from one value to another, of course based on the condition.

ABAP_740_Switch_Usage

Syntax

Simple SWITCH syntax is like something similar to this:

 
DATA(lv_status) = SWITCH #( lv_flag
                              WHEN 'X' THEN 'Passed'
                              ELSE 'failed'
                          ).
 

Things to remember

There are few things you want to remember when using the SWITCH operator.

  • The type of the return value would be determined automatically when you use the #. If compiler is unable to determine the type, you need to specify the explicit type like SWITCH string.
  • If the returned value is the object reference, the up-casting should be possible.
  • There should be at least on WHEN. Doesn’t make sense to use SWITCH without any WHEN πŸ™‚
  • The value specified after the WHEN should be β€œmatch-able” with the variable which is used for Switch

You can also use the LET expression, where you can define the local variables, which can be used in that particular context. More about LET expression coming soon.

Example 1 – Simple usage to return the Status based on Flag

When we use the above syntax, the output would be as failed.

 
DATA: lv_flag TYPE flag.
DATA(lv_status) = SWITCH #( lv_flag
                              WHEN 'X' THEN 'Passed'
                              ELSE 'failed'
                          ).
WRITE: lv_status.
 

Output of this simple example

ABAP_740_Switch_Example_1

Example 2 – Switch usage to replace character

Here, the start point is the string with ABCD. . . Also note that you can directly use the data reference variable without assigning it to field symbol.

 
DATA(lv_text) = NEW char10( 'ABCD@#@#' ).
DATA(lv_output) = NEW char10( ).
 
DO 10 TIMES.
  DATA(lv_offset) = NEW i( sy-index - 1 ).
  DATA(lv_char_part) = NEW char1( lv_text->*+lv_offset->*(1) ).
  DATA(lv_new_part) =
    SWITCH char1( lv_char_part->*
                  WHEN 'A' THEN 'Z'
                  WHEN 'B' THEN 'Y'
                  WHEN 'C' THEN 'X'
                  WHEN 'D' THEN 'W'
                  ELSE 0
    ).
  lv_output->*+lv_offset->*(1) = lv_new_part.
ENDDO.
 
WRITE: lv_output->*.
 

This logic would generate the output as ZYXW000000. Notice that the space is also replaced with 0 as there is no special logic to handle the character with space.

ABAP_740_Switch_Example_2

Example 3 – Using LET at SWITCH level

You can use the LET at the SWTICH level. This temporary variable can be used in the SWITCH to after THEN. You would need to specify the LET before the condition for which it should be used. And don’t forget the IN. Without IN the LET doesn’t have the context. The variable can only be used after the THEN.

 
DATA(lv_text) = NEW char10( 'ABCD@#@#' ).
DATA(lv_output) = NEW char10( ).
 
DO 10 TIMES.
  DATA(lv_offset) = NEW i( sy-index - 1 ).
  DATA(lv_char_part) = NEW char1( lv_text->*+lv_offset->*(1) ).
  DATA(lv_new_part) =
    SWITCH char1( LET x = '*' IN   "<<
                  lv_char_part->*
                  WHEN 'A' THEN 'Z'
                  WHEN 'B' THEN 'Y'
                  WHEN 'C' THEN 'X'
                  WHEN 'D' THEN 'W'
                  WHEN space THEN x    "<<
                  ELSE 0
    ).
  lv_output->*+lv_offset->*(1) = lv_new_part.
ENDDO.
 
WRITE: lv_output->*.
 

You would see this output when you runt this logic:

ABAP_740_Switch_Example_3

Example 4 – Using LET at SWITCH and WHEN

Similarly the LET can also be specified after the WHEN to create the local variables which can be used for WHEN local context. Compare this example with example 3.

 
DATA(lv_text) = NEW char10( 'ABCD@#@#' ).
DATA(lv_output) = NEW char10( ).
 
DO 10 TIMES.
  DATA(lv_offset) = NEW i( sy-index - 1 ).
  DATA(lv_char_part) = NEW char1( lv_text->*+lv_offset->*(1) ).
  DATA(lv_new_part) =
    SWITCH char1( LET x = '*' IN
                  lv_char_part->*
                  WHEN 'A' THEN 'Z'
                  WHEN 'B' THEN 'Y'
                  WHEN 'C' THEN 'X'
                  WHEN 'D' THEN 'W'
                  WHEN space THEN let y = x in y  "<<
                  ELSE 0
    ).
  lv_output->*+lv_offset->*(1) = lv_new_part.
ENDDO.
 
WRITE: lv_output->*.
 

Output for this logic is same as the example 3, as here I’m just replacing the value of y with x, in which x is set as * .

Table of Content – ABAP 740 Concepts

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

1 Comment

  • Thanks for the interesting share! Very helpful!

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.