Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

IF...ELSEIF...ELSE LOOP

Former Member
0 Likes
6,229

Hi gurus,

               I am calculating overdue days with the help of following loop

     if wa_data-pay_term = c01.

       overdue_days = overdue_days - 0.

       ELSEIF wa_data-pay_term = c02.

       overdue_days = overdue_days - 7.

          ELSEIF wa_data-pay_term = c03.

          overdue_days = overdue_days - 14.

              ELSEIF wa_data-pay_term = c04.

              overdue_days = overdue_days - 30.

                    ELSEIF wa_data-pay_term = c05.

                    overdue_days = overdue_days - 45.

                          ELSEIF wa_data-pay_term = c06.

                          overdue_days = overdue_days - 60.

                                ELSE.

                                overdue_days = overdue_days - 90.

    ENDIF.


But the loop is not working correctly. If wa_data-payterm = c04. it should subtract 40 instead it is subtracting 90. Please help me where i have gone wrong.

1 ACCEPTED SOLUTION
Read only

hardyp180
SAP Mentor
SAP Mentor
0 Likes
3,667

A CASE statement might be a more elegant way to code this as in

CASE wa_data-pay_term/

  WHEN c01.

    overdue_days = etc...

In ABAP 740 the SWITCH statement can make this even more compact, as you don't have to keep mentioning "overdue_days" in every branch.

Also, you should not use meaningless constants like C06. It should have name a name like C_RISKY_CUSTOMER so the code is easier to read.

Going even further, this should be a customising table (or BRF+ rule) in case the rules ever change e.g. a new customer group is added, or the value for Group 6 changes from 60 to 55 or something.

That way when (not if, when) the business rules change you don't have to change your program - this is known as the "open-closed" principle.

However, going back to your original question the only way I

F WA_DATA-PAYTERM = C04

could evaluate to false if is you have a spelling mistake in your definition of the constant C04.

Cheersy Cheers

Paul

6 REPLIES 6
Read only

jay_kumar8
Active Participant
0 Likes
3,667

Hi ,

First check in debugging mode what exactly value is coming in wa_data-pay_term

As per my understanding your code is correct.

Debug is the only thing.

Regards

Raj

Read only

former_member185613
Contributor
0 Likes
3,667

Hi,

Debugging would be the best way to get the problem that is occurring. Check the value of wa_data-pay_term and value of C_04 during debugging and see if they match.

Hope this helps,

~Athreya

Read only

ipravir
Active Contributor
0 Likes
3,667

Hi Sathy,

You can use CASE statement for the above logic.

DATA: lv_date type i.

CASE  lv_value.

     when 'c01'.

          lv_date = 0.

     when 'c02'.

          lv_date = 7.

     when 'c03'.

          lv_date = 14.

     when 'c04'.

          lv_date = 30.

.....

     WHEN Other.

          lv_date = 90.

ENSCASE.

overdue_days = overdue_days - lv_date.

Hope you are clear with logic.

Regards.

Praveer.

Read only

hardyp180
SAP Mentor
SAP Mentor
0 Likes
3,668

A CASE statement might be a more elegant way to code this as in

CASE wa_data-pay_term/

  WHEN c01.

    overdue_days = etc...

In ABAP 740 the SWITCH statement can make this even more compact, as you don't have to keep mentioning "overdue_days" in every branch.

Also, you should not use meaningless constants like C06. It should have name a name like C_RISKY_CUSTOMER so the code is easier to read.

Going even further, this should be a customising table (or BRF+ rule) in case the rules ever change e.g. a new customer group is added, or the value for Group 6 changes from 60 to 55 or something.

That way when (not if, when) the business rules change you don't have to change your program - this is known as the "open-closed" principle.

However, going back to your original question the only way I

F WA_DATA-PAYTERM = C04

could evaluate to false if is you have a spelling mistake in your definition of the constant C04.

Cheersy Cheers

Paul

Read only

rosenberg_eitan
Active Contributor
0 Likes
3,667

Hi,

I have the feeling that you can use CASE here.

Your code will look much better and easier to debug.

http://help.sap.com/abapdocu_740/en/abapcase.htm

Regards .

Read only

Former Member
0 Likes
3,667

Hi Sathyamurthy,

The code which you have written is correct but just check once in debugging what is happening and you can find the issue clearly.

Note: Instead of nested IF..ELSEIF you can move to CASE it increase performance of the program as well as it makes good readability to others.

Regards,

Chakradhar.