COND Operator instead of IF/ELSE in ABAP New Syntax


COND Operator instead of IF/ELSE in ABAP New Syntax

As case statements are used to evaluate only one variable condition at a time, we use if/else to consider multiple logical conditions.

Consider the below example.

DATA: lv_text(20). 
IF lv_vehicle = '01' AND lv_type = 'C'. 
 lv_text = 'Hyundai. 
ELSE. 
IF lv_vehicle ='02' AND lv_type = 'C'. 
 lv_text = 'KIA' 
ELSE. 
IF lv_vehicle ='03' AND lv_type = 'C'. 
 lv_text = 'Maruti'. 
 .. 
ENDIF.

In new syntax the above code can be written as

DATA(lv_text) = COND text20( 
 WHEN lv_vehicle ='01' AND lv_type = 'C' THEN 'Hyundai' 
 WHEN lv_vehicle ='02' AND lv_type = 'C' THEN 'KIA' 
 WHEN lv_vehicle ='03' AND lv_type = 'C' THEN 'Maruti').

So, if we observe the new syntax code, there is no need to specify the target variable again and again for every if, elseif block like old syntax and the target variable is declared on the fly using inline declaration. This reduces the code length.

Post a Comment

Previous Post Next Post