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 exit a loop

Former Member
0 Likes
5,340

How to exit from a loop. for eg

C = sy-tabix.

LOOP AT IT_GRAPH1.

D = SY-TABIX.

IF D GT C.

EXIT.

ENDIF.

<cond>

I want that when D becomes greater then C then it should exit the whole loop processing.

But this code is not working even if D becomes greater then C still the loop processing continue.

Please Help.

How to exit from a loop. for eg

C = sy-tabix.

LOOP AT IT_GRAPH1.

D = SY-TABIX.

IF D GT C.

EXIT.

ENDIF.

<cond>

I want that when D becomes greater then C then it should exit the whole loop processing.

But this code is not working even if D becomes greater then C still the loop processing continue.

Please Help.

9 REPLIES 9
Read only

Former Member
0 Likes
2,044

hi ,

if u want to exit from the loop give stop.

Read only

Former Member
0 Likes
2,044

hi priya

instead of if statement try once check statment

it will work

thanks and regards

snehi chouhan

Read only

Former Member
0 Likes
2,044

hi

assign some value to varible C not Sy-tabix.

or try with condition operator GE.

this will work. plz reward me if it helps out.

regars,

munvar.

Read only

Former Member
0 Likes
2,044

Hi,

Check the below logic.

data: c like sy-tabix, d like c.

data: begin of itab occurs 0,

num type i,

end of itab.

do 5 times.

itab-num = sy-index.

append itab.

enddo.

C = 2 ."sy-tabix.

LOOP AT ITAB.

D = SY-TABIX.

IF D GT C.

EXIT.

ENDIF.

write:/ itab-num.

ENDLOOP.

Read only

0 Likes
2,044

C = sy-tabix.

LOOP AT IT_GRAPH1.

D = SY-TABIX.

IF D GT C.

EXIT.

ENDIF.

IF IT_GRAPH1-type = 'DO'.

M = M + 1.

ELSEIF IT_GRAPH1-type = 'ENDDO'.

N = N + 1.

ELSEIF IT_GRAPH1-type = 'LOOP'.

L = L + 1.

ELSEIF IT_GRAPH1-type = 'ENDLOOP'.

E = E + 1.

ELSEIF IT_GRAPH1-type = 'IF'.

I = I + 1.

ELSEIF IT_GRAPH1-type = 'ENDIF'.

P = P + 1.

ELSEIF IT_GRAPH1-type = 'CASE'.

R = R + 1.

ELSEIF IT_GRAPH1-type = 'ENDCASE'.

S = S + 1.

ENDIF.

ENDLOOP.

HII THIS IS MY CODE....the given logic is not working here

Read only

0 Likes
2,044

I can't see anything wrong with that code. 'EXIT' will exit a loop. Are you looping within a loop so that C is reset each time? It is worth break-pointing and checking what happens and what values are being set.

Read only

0 Likes
2,044

Firstly Do not Use the variable names as C or D , N as they are the types of declaration in ABAP as well ,

Change the names and try your Code ,, Logically should work .

Or else .

Instead of

IF D GT C.

EXIT.

ENDIF.

use ,

While D_temp LT C_temp .

IF IT_GRAPH1-type = 'DO'.

M = M + 1.

ELSEIF IT_GRAPH1-type = 'ENDDO'.

N = N + 1.

ELSEIF IT_GRAPH1-type = 'LOOP'.

L = L + 1.

ELSEIF IT_GRAPH1-type = 'ENDLOOP'.

E = E + 1.

ELSEIF IT_GRAPH1-type = 'IF'.

I = I + 1.

ELSEIF IT_GRAPH1-type = 'ENDIF'.

P = P + 1.

ELSEIF IT_GRAPH1-type = 'CASE'.

R = R + 1.

ELSEIF IT_GRAPH1-type = 'ENDCASE'.

S = S + 1.

ENDIF.

ENDWHILE .

regards

Badari,

Read only

peter_ruiz2
Active Contributor
0 Likes
2,044

hi,

try this code


C = sy-tabix.
LOOP AT IT_GRAPH1 TO C.
 <processing goes here>
ENDLOOP.

or this one


C = sy-tabix.
LOOP AT IT_GRAPH1.
  D = SY-TABIX.
  IF D GT C.
    EXIT FROM STEP-LOOP.
  ENDIF.
ENDLOOP.

regards,

Peter

Read only

Former Member
0 Likes
2,044

Hi Priya,

at first glance your code should work. The only thing I can think of is something in the typing of variables C and D that make the comparison not work.

Can you give us your definition of C and D?

Greetings,

Gert.