2010 Apr 20 6:48 PM
Hi All,
I am facing a strange situation where in I have to exit a LOOP when I condition is met & have to do further processing as follows:-
LOOP AT ITAB INTO WA.
gv_index = sy-tabix.
IF wa-rsnum NE space.
CLEAR wa_out2.
READ TABLE it_out2 INTO wa_out2 WITH KEY doc_id = wa-rsnum
ln_itm = wa_-rspos
ELSE.
CLEAR wa_out2.
LOOP AT it_out2 INTO wa_out2 WHERE ct_id =wa-empl_id
AND mat_id = wa-matnr
AND tlkt_qty = wa-tlkt_quan
AND kostl = wa-kostl
AND lstar = wa-lstar.
IF wa_out2-doc_id IS INITIAL.
* I have to exit loop here & do further processing of code written after first endloop..*
ENDIF.
SELECT SINGLE rsnum FROM zmm_toolkit INTO lv_rsnum
WHERE rsnum EQ wa_out2-doc_id.
IF sy-subrc = 0.
CLEAR wa_out2.
CONTINUE.
ENDIF.
ENDLOOP.
*further processing here.*
endloop.
Regards
Abhii
Edited by: Rob Burbank on Apr 20, 2010 2:15 PM
2010 Apr 20 6:51 PM
2010 Apr 20 6:58 PM
Sorry Rich,
Here If I use EXIT it goes to the next loop pass but I want it to execute the code after the first ENDLOOP.
Please reply
Abhii
2010 Apr 20 7:06 PM
>
> Here If I use EXIT it goes to the next loop pass but I want it to execute the code after the first ENDLOOP.
LOOP ..."Loop1
LOOP... "Loop2
EXIT.
ENDLOOP. "Loop2
ENDLOOP. "Loop1
Whenever the Loop2 encounters an EXIT statement, processing will resume after the ENDLOOP of Loop2. Next loop pass for Loop2 will be triggered for CONTINUE statement.
I doubt this behaviour has any aberration for nested loops.
BR,
Suhas
2010 Apr 20 7:26 PM
Hi Abhil. The EXIT will only exit the first loop. See in this example.
DATA: lt_tab1 TYPE TABLE OF string.
DATA: lt_tab2 TYPE TABLE OF string.
DATA: lv_str1 TYPE string.
DATA: lv_str2 TYPE string.
lv_str1 = 'This is loop 1, record 1'. APPEND lv_str1 TO lt_tab1.
lv_str1 = 'This is loop 1, record 2'. APPEND lv_str1 TO lt_tab1.
lv_str2 = 'This is loop 2, record 1'. APPEND lv_str2 TO lt_tab2.
lv_str2 = 'This is loop 2, record 2'. APPEND lv_str2 TO lt_tab2.
LOOP AT lt_tab1 INTO lv_str1.
LOOP AT lt_tab2 INTO lv_str2.
IF sy-subrc = 0. " Don't want to write anything here, so EXIT!!!
EXIT.
ENDIF.
WRITE:/ lv_str2.
ENDLOOP.
* Always want to write this.
WRITE:/ lv_str1.
ENDLOOP.
Regards,
Rich Heilman
2010 Apr 20 8:30 PM
Here If I use EXIT it goes to the next loop pass but I want it to execute the code after the first ENDLOOP.
This is a bit confusing, maybe you should be more specific. The EXIT command seems to do what you want. I.e. it terminates the inner loop and continues with the coding after the inner loop, which you had tagged as <em>further processing here</em> that you wanted to execute.
I kind of start to suspect that you somehow want to exit the outer loop as well, even though in there's iterations left. If that's the case I see essentially just one option: Define a boolean flag (using type ABAP_BOOL), which you set before the outer loop to ABAP_FALSE (I know it's redundant, but I think it's good style and actually increases readability). Then set the flag to ABAP_TRUE in your inner loop before you do the EXIT command as indicated by all other posters. You can then evaluate that flag in the outer loop wherever you want and simply EXIT there once the flag is set.
If that's what you want I don't think there's any other (reasonable) way. Since I've often seen crazy suggestions on SDN, let me point out one other really poor solution: You could wrap your functionality in methods and utilize an exception to exit both loops (by appropriately placing the TRY-ENDTRY block). However, this is poor programming style as exceptions should never be used to handle <em>normal</em> program flows (i.e. as the name says, they should be reserved for <em>exceptional</em> situations).
2010 Apr 26 11:03 AM
Hi Rich & all,
Thanks for the answers.
I have resolved this by using DO......ENDDO as below :-
DO.
CLEAR wa_out2.
READ TABLE it_out2 INTO wa_out2
WITH KEY ct_id = wa_toolkit-empl_id
mat_id = wa_toolkit-matnr
tlkt_qty = wa_toolkit-tlkt_quan
kostl = wa_toolkit-kostl
lstar = wa_toolkit-lstar.
IF sy-subrc = c_0.
IF wa_out2-doc_id IS INITIAL.
EXIT.
ENDIF.
SELECT SINGLE rsnum FROM zmm_toolkit INTO lv_rsnum
WHERE rsnum EQ wa_out2-doc_id.
IF sy-subrc = 0.
DELETE TABLE it_out2 FROM wa_out2.
* CLEAR wa_out2.
CONTINUE.
ELSE.
EXIT.
ENDIF.
ELSE.
EXIT.
ENDIF.
ENDDO.
Regards
Abhii
2010 Apr 20 6:53 PM
2010 Apr 20 8:54 PM
2010 Apr 20 9:09 PM
I think it's moot because the code won't pass a syntax check.
ENDIF, ENDLOOP and ENDSELECT are either misplaced or missing entirely.
LOOP.
IF.
ELSE.
LOOP.
IF.
ENDIF.
SELECT.
IF.
ENDIF.
ENDLOOP.
ENDLOOP.
Rob
Edited by: Rob Burbank on Apr 20, 2010 4:11 PM
2010 Apr 20 9:09 PM
I have another Idea that is not very nice but should work:
put the EXIT-statement in the inner loop AND set a flag.
Then check the flag value after the ENDLOOP of the inner loop. If it is true, put another EXIT-statement.
Something like this:
Loop at itab into wa.
flag = false.
...
loop at itab2 into wa2.
if condition is met.
EXIT.
flag = true
endif.
endloop.
if flag = true.
exit.
endif.
endloop
don't forget to put the flag on "false" again. I think the statement at the beginning of the first loop should do it. But please check again.
Yours
Anja
Edited by: Anja Dillinger on Apr 20, 2010 10:39 PM
2010 Apr 20 9:35 PM
From what I understand I guess you want to exit both the loops when certain conditions are not met.
I mean Instead of
ENDLOOP.
further processing here.
endloop.
your might want
ENDLOOP.
endloop.
further processing here.
If that is the case you confused everyone. Change your code as follows to serve the purpose.
l_subrc like sy-subrc.
LOOP AT ITAB INTO WA.
gv_index = sy-tabix.
IF wa-rsnum NE space.
CLEAR wa_out2.
READ TABLE it_out2 INTO wa_out2 WITH KEY doc_id = wa-rsnum
ln_itm = wa_-rspos
ELSE.
CLEAR l_subrc.
CLEAR wa_out2.
LOOP AT it_out2 INTO wa_out2 WHERE ct_id =wa-empl_id
AND mat_id = wa-matnr
AND tlkt_qty = wa-tlkt_quan
AND kostl = wa-kostl
AND lstar = wa-lstar.
IF wa_out2-doc_id IS INITIAL.
I have to exit loop here & do further processing of code written after first endloop..*
l_subrc = 4.
EXIT.
ENDIF.
SELECT SINGLE rsnum FROM zmm_toolkit INTO lv_rsnum
WHERE rsnum EQ wa_out2-doc_id.
IF sy-subrc = 0.
CLEAR wa_out2.
CONTINUE.
ENDIF.
ENDLOOP.
IF l_subrc NE 0.
CLEAR l_subrc.
EXIT.
ENDIF.
endloop.
further processing here.
-
-
Edited by: Santha Badhri on Apr 20, 2010 10:37 PM
Edited by: Santha Badhri on Apr 20, 2010 10:40 PM
Edited by: Santha Badhri on Apr 20, 2010 10:41 PM
2010 Apr 26 12:25 PM
hi,
IF wa_out2-doc_id IS INITIAL.
I have to exit loop here & do further processing of code written after first endloop..*
EXIT. "<<- Use EXIT statement
ENDIF.
Regards,