ABAP Control Flow Statements COND/SWITCH


SWITCH and COND offer a special form of conditional program flow. Unlike IF and CASE, they respresent different values based on an expression rather than executing statements. That's why they count as functional.


Whenever multiple conditions have to be considered, COND can do the job. The syntax is fairly simple:

COND <type>( 
    WHEN <condition> THEN <value> 
    [ ELSE <default> | throw <exception> ]


" Set screen element active depending on radio button
screen-active = COND i(
    WHEN p_radio = abap_true THEN 1
    ELSE 0 " optional, because type 'i' defaults to zero

" Check how two operands are related to each other
" COND determines its type from rw_compare
rw_compare = COND #( 
    WHEN op1 < op2 THEN 'LT'
    WHEN op1 = op2 THEN 'EQ'
    WHEN op1 > op2 THEN 'GT' 


SWITCH is a neat tool for mapping values, as it checks for equality only, thus being shorter than COND in some cases. If an unexpected input was given, it is also possible to throw an exception. The syntax is a little bit different:

SWITCH <type>( 
    WHEN <value> THEN <new_value> 
    [ ELSE <default> | throw <exception> ]


DATA(lw_language) = SWITCH string(
    WHEN 'E' THEN 'English'
    WHEN 'D' THEN 'German'
    " ...
    ELSE THROW cx_sy_conversion_unknown_langu( )