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

Loop...Endloop does not serve the purpose

Former Member
0 Likes
2,006

Dear All,

I have one internal table with 10000 records . I want to read a specific record with certain WHERE clause with logical operators.

Now my requirement is that as soon as that condition is satisfied ,

>control should go to NEXT STATEMENT AFTER Endloop BUT NOT to the next loop pass so that it will not read

>remaining entries.

Using EXIT takes us to Next Loop Pass which I want to avoid and come out of the LOOP ..ENDLOOP,

Using READ TABLE does not allow logical operators like NE, GT,LE...

What can be done? Suggestions are most welcome.

Thanks...........

Edited by: Matt on Mar 13, 2009 8:21 AM - fix formatting

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,885

Try using STOP inside the loop...endloop.Stop will come out of the loop.

Also try settings Flag at appropriate place if not working paste the codes.

Regards,

Gurpreet

15 REPLIES 15
Read only

Former Member
0 Likes
1,886

Try using STOP inside the loop...endloop.Stop will come out of the loop.

Also try settings Flag at appropriate place if not working paste the codes.

Regards,

Gurpreet

Read only

former_member242255
Active Contributor
0 Likes
1,885

you can use a flag and write exit and once that loop exits and comes to another loop..

now check the flag again and if the falg is on,then exit again...

this should serve the purpose..

loop 1

loop2

flag = X

EXIT

endloop

if flag = 'x'

EXIT

endif

Endloop

endloop

Edited by: Sravan Kumar on Mar 13, 2009 7:00 AM

Read only

keerthy_k
Product and Topic Expert
Product and Topic Expert
0 Likes
1,885

Hi,

EXIT statement , if used inside a loop..will exit the loop and will goto the next statment after the endloop statement. Please check if u are using a nested loop like loop endloop inside another loop endloop.

It works perfect for one of my programs.

see the code below..

data: itab type table of spfli,

wa type spfli,

itab1 type table of sflight.

select * from spfli into table itab

where carrid like 'A%'.

loop at itab into wa where carrid = 'AA'.

write:/ wa-carrid.

exit.

endloop.

write:/ wa-connid.

Keerthi.

Edited by: Keerthy K on Mar 13, 2009 7:02 AM

Read only

Former Member
0 Likes
1,885

Hi;

Put

LOOP where <condition>.

ENDLOOP

Next Condition

LOOP would continue till the condition meets and would be in next condition,

Regards

Shashi

Read only

Former Member
0 Likes
1,885

do it this way..


loop itab into wa_tab.


if wa-tab-matnr = 'XYZ'.   " Write Where Logic in If condition instaed of where in loop.

exit.
endif.

endloop.

Read only

sarbajitm
Contributor
0 Likes
1,885

Use Check like

loop at itab.

check bbb <> ' '.

endloop.

where bbb is a field of itab. when bbb will be blank the loop terminates.

Hope it solve your problem.

Regards.

Sarbajit

Read only

matt
Active Contributor
0 Likes
1,885

>

> Use Check like

>

> loop at itab.

> check bbb <> ' '.

> endloop.

>

> where bbb is a field of itab. when bbb will be blank the loop terminates.

>

> Hope it solve your problem.

>

> Regards.

>

> Sarbajit

WRONG

CHECK cond works like

IF cond.

CONTINUE.

ENDIF.

Read only

0 Likes
1,885

Hi Matt

I mean by the follwing code

loop at itab.

check itab-bbb = ' ' .

endloop.

The loop iterates as long as the check condition will be true. But terminates loop iteration if the check condition become false. Is it Wrong?

Please Reply.

What you have written CHECK = IF Continue ENDIF that is true also.

regards.

sarbajit.

Read only

dev_parbutteea
Active Contributor
0 Likes
1,885

Hi,

Here you can use exit.

Actually in a loop:

exit --> execution goes out of loop, to statement next to endloop

continue --> execution goes to next loop phase

stop --> it acts a exit but in reports execution will go to next event.

Regards.

Read only

Former Member
0 Likes
1,885

You can use EXIT statement here.

Loop at itab .

If <Condition>.

Exit.

Endif.

Endloop.

Regards,

Joan

Read only

Former Member
0 Likes
1,885

Hi,

Use EXIT in the loop. EXIT dose not take you to next loop pass but it takes you out of loop. CONTINUE takes you to next loop pass.

Regards,

Pranaya

Read only

vishal_sharda2
Participant
0 Likes
1,885

Hi,

EXIT statement is the most recommended option fr your problem.

When EXIT is encountered, control comes out of the loop to the next statement after ENDLOOP. On contrary, CONTINUE statement goes to the next pass of the loop.

Try EXIT. It will solve your problem.

Regards,

Vishal.

Read only

matt
Active Contributor
0 Likes
1,885

If you need to cascade an exit from an inner loop, you should do it like this.

LOOP AT itab1.
  LOOP AT itab2.
    LOOP AT itab3.
      IF cond.
        l_exit_flag = 'X'.
        EXIT.
      ENDIF.
    ENDLOOP.
    IF l_exit_flag IS NOT INITIAL.
      EXIT.
    ENDIF.
  ENDLOOP.
  IF l_exit_flag IS NOT INITIAL.
    EXIT.
  ENDIF.
ENDLOOP.

EXIT does NOT force a new loop iteration.

Read only

Former Member
0 Likes
1,885

Hi Ubhaka

The exit statement takes you out of the current loop.You can set some flag(in the first loop) when your condition is satisfied and can come out of any number of loops-simplest of all .Also using stop will take you to the end of selection event.It all depends on your reqirement.Word of caution-Check and stop will not set the sy-subrc.If you want to use it set numeric value to it before exiting.Hope this solve your query.

Regards.

Read only

Former Member
0 Likes
1,885

issue closed. thanks......