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

how to code ..simple if loop statement ?

Former Member
0 Likes
1,130

HI ABAP gurus,

I need to determine the category field based on these conditions. can anyone suggest the code for the same ?

category = C1  if EKPO-PSTYP is equal to 1 and EKPO-KONNR is not equal to NULL.

category = c2 if EKPO-PSTYP is equal to 1 and EKPO-KONNR is qual to NULL.

category = C3 if EKPO-PSTYP is not equal to 1 and EKPO-KONNR is equal to NULL 

Category = C4 if EKPO-PSTYP is not equal to 1 and EKPO-KONNR is not qual to NULL and

category = C5 if EBAN-ESTKZ = R.

All these C1 to C5 will be populated in a single field based on the above conditions. I need to write a code for this category field.

I dont know how to handle the if and endif and else if in a single statement.

I can try, please correct it gurus.

if EKPO-PSTYP is equal to 1 and EKPO-KONNR is not equal to NULL then category = C1

else if

if EKPO-PSTYP is equal to 1 and EKPO-KONNR is qual to NULL then category = C2

else if

if EKPO-PSTYP is not equal to 1 and EKPO-KONNR is equal to NULL then category = C3

else if

if EKPO-PSTYP is not equal to 1 and EKPO-KONNR is not qual to NULL then category = C4

else if

EBAN-ESTKZ =  R then category = C5.

endif.

endif.

endif.

endif.

I am not sure about these if and endif concept. Please correct my code and suggest the original code in ABAP please. I appreciate it.

thank You,

DR

10 REPLIES 10
Read only

former_member282823
Active Participant
0 Likes
1,088

Hi,

There will be only only if and endif rest everything will be in elseif.

if  EKPO-PSTYP eq 1 and EKPO-KONNR is not initial.

   category = c1.

elseif EKPO-PSTYP eq 1 and EKPO-KONNR is initial.

category = C2.

elseif EKPO-PSTYP ne 1 and EKPO-KONNR is initial.

category = C3.

elseif EKPO-PSTYP ne 1 and EKPO-KONNR is not initial.

category = C4.

elseif EBAN-ESTKZ =  R.

category = C5.

endif.

Read only

0 Likes
1,088

Thank You so much Ramesh. I appreciate it. You are the best. I wanna add you and post my ABAP queries to you. You can suggest if you are free. Is that OK

Cheers,

DR

Read only

0 Likes
1,088

thanks for the compliment..you can ask me if you have any questions regarding ABAP queries...Incase if i am not available in SDN you can send me mail @rameshkrishnamani@gmail.com.

Regards,

Ramesh.

Read only

Former Member
0 Likes
1,088

HI,

       IF EKPO-PSTYP EQ 1 and EKPO-KONNR is not equal to NULL .

                 category = C1 .

      elseif EKPO-KONNR is  equal to NULL .

                   category = C2 .

      endif.

  IF EKPO-PSTYP ne 1 and EKPO-KONNR is  equal to NULL .

                 category = C3.

      elseif EKPO-KONNR is not  equal to NULL .

                   category = C4 .

      elseif EBAN-ESTKZ = R.

                       category = C5

endif..

Read only

0 Likes
1,088

Oh my god another smart answer. You guys are too fast. I wanna learn ABAP....and I am trying. ...but its tough for a new bee guys.....so just a question....can i use either of the code's or any suggestions....just a question.....do i need to put skip or exit after the first endif.....so that it wont check for c3,c4,c5 when one of the first condition is met or not needed.

thanks,

DR

Read only

0 Likes
1,088

Hi Daniel,

IF ekpo-pstyp EQ 1.

   IF ekpo-konnr IS NOT INITIAL.

     category = c1.

   ELSEIF ekpo-konnr IS NOT INITIAL.

     category = c2.

   ENDIF.

ELSEIF ekpo-pstyp NE 1.

   IF ekpo-konnr IS NOT INITIAL.

     category = c3.

   ELSEIF ekpo-konnr IS NOT INITIAL.

     category = c4.

   ENDIF.

ENDIF.

Regards,

Jyoti.

Read only

0 Likes
1,088

Hi Jyoti,

How about C5 ?

THanks,

Read only

0 Likes
1,088

oh looks like missed some...sorry for that.

for C5 you have to add another elseif statement. Here the example code

IF ekpo-pstyp EQ 1.

   IF ekpo-konnr IS NOT INITIAL.

     category = c1.

   ELSEIF ekpo-konnr IS NOT INITIAL.

     category = c2.

   ENDIF.

ELSEIF ekpo-pstyp NE 1.

   IF ekpo-konnr IS NOT INITIAL.

     category = c3.

   ELSEIF ekpo-konnr IS NOT INITIAL.

     category = c4.

   ENDIF.

ELSEIF eban-esktz IS NOT INITIAL.

   category = c5.

ENDIF.

Regards,

Jyoti.

Read only

Former Member
0 Likes
1,088

Hi Daniel,

Please find sample code.

Loop at it_mara into wa_mara.

if wa_mara-material = 'XYX'.

do something.

else.

do something

endif.

endloop.

Read only

Former Member
0 Likes
1,088

Hey Daniel,

          I guess there are many ways to write If and endif but as a Standard Coding Practice they'll always suggest you to modularise things.

If EKPO-PSTYP = 1.

   If EKPO-KONNR = null.

     category = c2

   Else.

     category = C1

   endif.

 

Elseif EKPO-PSTYP <> 1.

   If EKPO-KONNR = null.

     category = c3

   Else.

     category = C4

   Endif.

Elseif EBAN-ESTKZ = R.

   

   category = C5.

endif.