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

DO .... END DO problem

Former Member
0 Likes
2,640

hi experts

I am looping like this

Do L1 times .... END DO

now the value of L1 is first fixed to 10 and after this i want to change the value of L1 inside the loop to 1... and then the loop should run only 1 time...

Is this possible, I have tried this but not able to get the way I want

Please suggest me an alternative to this

The code goes as follows

REPORT Z_SEARCH_STRING .

DATA: FI_NAME TYPE STRING, LA_NAME TYPE STRING, L1 TYPE I, L2 TYPE I.

DATA: F_FNAME TYPE STRING, F_LNAME TYPE STRING.

DATA: Z TYPE I, X TYPE I.

DATA: NUM(1), flag(1).

FI_NAME = 'th is S0101 2 5'.

CONDENSE FI_NAME NO-GAPS.

*This will remove the gaps then

L1 = STRLEN( FI_NAME ).

L2 = L1.

*this will calculate the length which helps for looping

X = 0.

<b>DO L1 TIMES.

F_FNAME = FI_NAME+X(1).

Z = X.

IF F_FNAME = 'S' OR F_FNAME = 'W'.

Z = Z + 1.

NUM = FI_NAME+Z(1).

IF NUM = '1' OR

NUM = '2' OR

NUM = '3' OR

NUM = '4' OR

NUM = '5' OR

NUM = '6' OR

NUM = '7' OR

NUM = '8' OR

NUM = '9' OR

NUM = '0'.

Z = Z + 1.

NUM = FI_NAME+Z(1).

IF NUM = '1' OR

NUM = '2' OR

NUM = '3' OR

NUM = '4' OR

NUM = '5' OR

NUM = '6' OR

NUM = '7' OR

NUM = '8' OR

NUM = '9' OR

NUM = '0'.

Z = Z + 1.

NUM = FI_NAME+Z(1).

IF NUM = '1' OR

NUM = '2' OR

NUM = '3' OR

NUM = '4' OR

NUM = '5' OR

NUM = '6' OR

NUM = '7' OR

NUM = '8' OR

NUM = '9' OR

NUM = '0'.

Z = Z + 1.

NUM = FI_NAME+Z(1).

IF NUM = '1' OR

NUM = '2' OR

NUM = '3' OR

NUM = '4' OR

NUM = '5' OR

NUM = '6' OR

NUM = '7' OR

NUM = '8' OR

NUM = '9' OR

NUM = '0'.

Z = Z + 1.

LA_NAME = FI_NAME+X(5).

L2 = L1 - Z.

IF L2 = 0.

WRITE:/ LA_NAME.

STOP.

ELSE.

WRITE:/ LA_NAME.

FI_NAME = FI_NAME+Z(L2).

ENDIF.

L1 = STRLEN( FI_NAME ).

flag = 'x'.

ENDIF.

ENDIF.

ENDIF.

ENDIF.

ENDIF.

IF FLAG <> 'x'.

X = X + 1.

ELSE.

X = 0.

ENDIF.

CLEAR FLAG.

ENDDO.</b>

IF L2 = L1.

WRITE:/ 'no string found'.

10 REPLIES 10
Read only

Former Member
0 Likes
1,896

Hi,

U can try this...

L = L1 + 1.

Do L times.

-


---

ENDDO.

Sreedhar

Read only

0 Likes
1,896

Hi Rahul,

why don't you use EXIT to come out of the DO loop whenever your condition is met?

Regards,

Suresh Datti

Read only

Former Member
0 Likes
1,896

Hi,

Best Way to do this is use DO........ENDDO.


 Do 
   ............  logic for code.
...................
   IF condition.
     EXIT.
   ENDIF.
 ENDDO.
 

u can run any nuber of time and exit only at particular condition.

Read only

Former Member
0 Likes
1,896

Hi,

use this logic

l1 = 10.

do l1 times.

if sy-index = 10.

l1 = 1.

endif.

enddo.

Regards,

Amole

Read only

Former Member
0 Likes
1,896

Hi Rahul,

Although using a exit inside loop on if condition would be a better idea, try using Continue and Exit inside your Do..endDo loop.. instead of using STOP..

Try using exit instead of Stop..

Wat is the output u r expecting n wat r u getting??

Regards,

Tanveer.

<b>Please mark helpful answers</b>

Read only

Former Member
0 Likes
1,896

Try this ...

clear lc_flag.

do l1 times.

if lc_flag eq 'X'.

exit.

endif.

if <condition>.

move 'X' to lc_flag.

endif.

enddo.

Regards,

Felipe Cunha.

Read only

0 Likes
1,896

What are you trying to achieve?

Read only

0 Likes
1,896

I will be searching the string for a pattern, which begins with S or W and then later followed by 4 digits

so once the first pattern is retireved from the main string there is no problem to get it and LA_NAME which will be the rest of the string...

now the loop needs to run only l1 times which is the length of the new string, so that i can search for another pattern and then exit when length becomes less than 5

Read only

0 Likes
1,896

Hi Rahul,

try this piece..


report  zstring                                 .
data: w_pos_w type i,
      w_pos_s type i,
      w_len type i,
      w_name type string.
parameters: p_str(200).

w_len = strlen( p_str ).
search p_str for 'S' and mark.
if sy-subrc eq 0.
  w_pos_s = sy-fdpos.
endif.
search p_str for 'W' and mark.
if sy-subrc eq 0.
  w_pos_w = sy-fdpos.
endif.

if w_pos_s lt w_pos_w or w_pos_w eq 0.
  w_name = p_str+w_pos_s.
else.
  w_name = p_str+w_pos_w.
endif.

write: / w_name.

Regards,

Suresh Datti

Read only

Former Member
0 Likes
1,896

Hi rahul,

exit prevents further loop processing and exits immediately out of the current loop. It does not terminate the program when inside of a do loop. Processing continues at the next executable statement after the enddo.

You can create an infinite loop by coding do without any additions. In that situation, use exit within the loop to terminate loop processing.

do award if helpful.

regards,

keerthi.