cancel
Showing results for 
Search instead for 
Did you mean: 

how to combine/simplify these multiple IF Statements?

sh4il3sh
Participant
0 Kudos

I have below multiple IF statements which I want to see combined,
I tried using ELSEIF and CASE but its not applicable here because the comparison operator is <> instead of =

Below is the code:

 

        IF <ls_load>-atnam <> 'AA'.
          IF VALUE #( lt_num[ charact = 'AA' ]-value_from OPTIONAL ) IS INITIAL.
            DELETE lt_num WHERE charact = 'AA'.
          ENDIF.
        ENDIF.

        IF <ls_load>-atnam <> 'BB'.
          IF VALUE #( lt_num[ charact = 'BB' ]-value_from OPTIONAL ) IS INITIAL.
            DELETE lt_num WHERE charact = 'BB'.
          ENDIF.
        ENDIF.

        IF <ls_load>-atnam <> 'CC'.
          IF VALUE #( lt_num[ charact = 'CC' ]-value_from OPTIONAL ) IS INITIAL.
            DELETE lt_num WHERE charact = 'CC'.
          ENDIF.
        ENDIF.

 

 

Accepted Solutions (0)

Answers (3)

Answers (3)

Tomas_Buryanek
Active Contributor

Hello, consider creating a method in this case. All three conditions looks same, just parameter 'AA'.

NISHANT_R
Explorer
0 Kudos

Hi ,

I combined multiple if's to single if using else if . Try it.

Refer the screenshot for the code.

 

 

sajid-khan
Explorer
0 Kudos
@NISHANT_R your code is logically different than the code posted by the author.
NISHANT_R
Explorer
0 Kudos
@sajid-khan I think that the logic is same but the code construct is different
sajid-khan
Explorer
0 Kudos
@NISHANT_R Nope, your code will produce different result than the code posted by the author.
Tomas_Buryanek
Active Contributor
0 Kudos
You are using ELSEIF which is not used in OPs code.
sajid-khan
Explorer
0 Kudos

If your aim is to eliminate code repetition by combining IF's, you can use a simple LOOP:

 

LOOP AT VALUE TT_ACTION_CODES( ( 'AA' ) ( 'BB' ) ( 'CC' ) ) ASSIGNING FIELD-SYMBOL(<value>).
  IF <ls_load>-atnam <> <value>.
    IF VALUE #( lt_num[ charact = <value> ]-value_from OPTIONAL ) IS INITIAL.
      DELETE lt_num WHERE charact = <value>.
    ENDIF.
  ENDIF.
ENDLOOP.

 

Regards,
Sajid Khan