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.
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
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.
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:
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
- ABAP 740 β NEW Operator to instantiate the objects
- ABAP 740 β NEW Operator to create ITAB entries
- ABAP 740 β VALUE Operator to create ITAB entries
- ABAP 740 β Table Expressions to Read & Modify ITAB line
- ABAP 740 β LINE_EXISTS to check record in ITAB
- ABAP 740 β Meshes β A new complex type of Structures
- ABAP 740 β Mesh Path β Forward and Inverse Association
- ABAP 740 β FOR Iteration Expression
- ABAP 740 SWITCH – Conditional Operator
- ABAP 740 β LOOP AT with GROUP BY
- ABAP 740 – Is CONSTANT not a Static Attribute anymore?
- SALV IDA (Integrated Data Access) – Introduction
- SALV IDA – Selection Conditions
- SALV IDA – New Calculated Fields
- SALV IDA – Column Settings
- SALV IDA – Add and Handle Hotspot (Hyperlink)
Thanks for the interesting share! Very helpful!