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

difference between while & do statement in abap editor

Former Member
0 Likes
5,564

hi guru,

difference between while & do statement in abap editor.

please tell me.

regards.

asis

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
2,477

HI,

While statement: runs till when the logical expresion you have give fails

While <you expression>

write : <TEXT> .................. This output is written till the logical expression fails..

endwhile.

<b>Do statement</b>

i = 1.

DO 5 times.

i = i + sy-index. " syindex is system variable which increases for every do loop..

"eg in C we need to write increment operation for(i=1, i=5, i++)

"but here SAP does the property.

write : / i.

ENDDO.

output :

2

3

4

5

6.

rewards points if okay

regards,

nazeer

2 REPLIES 2
Read only

Former Member
0 Likes
2,477

HI!

While:

Effect

Repeats the processing enclosed between the WHILE and ENDWHILE statements as long as the logical expression logexp is true.

Checks the condition before each loop pass. If it is no longer true, processing resumes after ENDWHILE.

You can use the CONTINUE statement to leave the current loop pass prematurely and skip to the next loop pass.

Example

DATA: SEARCH_ME TYPE I,

MIN TYPE I VALUE 0,

MAX TYPE I VALUE 1000,

TRIES TYPE I,

NUMBER TYPE I.

SEARCH_ME = 23.

WHILE NUMBER <> SEARCH_ME.

ADD 1 TO TRIES.

NUMBER = ( MIN + MAX ) / 2.

IF NUMBER > SEARCH_ME.

MAX = NUMBER - 1.

ELSE.

MIN = NUMBER + 1.

ENDIF.

ENDWHILE.

The above code performs a (binary) search for the "unknown" number SEARCH_ME which lies between MIN and MAX. TRIES contains the number of attempts needed to find it.

DO

Effect

Repeats the processing enclosed by the DO and ENDDO statements until the loop is terminated by EXIT, STOP or REJECT.

You can use the CONTINUE statement to end the current loop pass prematurely and continue with the next loop pass.

The system field SY-INDEX counts the number of loop passes, starting from 1. You can nest DO loops. When the processing leaves an inner DO loop, the value of SY-INDEX belonging to the outer DO loop is restored.

Example

DO.

WRITE: / 'SY-INDEX - Begin:', (3) SY-INDEX.

IF SY-INDEX = 10.

EXIT.

ENDIF.

WRITE: 'End:', (3) SY-INDEX.

ENDDO.

This DO loop outputs 9 lines of the form

"SY-INDEX - Begin: n End: n ".

Here, n stands for the numbers 1 to 9.

The last line displayed is

"SY-INDEX - Begin: 10".

On the 10th pass, the loop is terminated.

Regards

Tamá

Read only

Former Member
0 Likes
2,478

HI,

While statement: runs till when the logical expresion you have give fails

While <you expression>

write : <TEXT> .................. This output is written till the logical expression fails..

endwhile.

<b>Do statement</b>

i = 1.

DO 5 times.

i = i + sy-index. " syindex is system variable which increases for every do loop..

"eg in C we need to write increment operation for(i=1, i=5, i++)

"but here SAP does the property.

write : / i.

ENDDO.

output :

2

3

4

5

6.

rewards points if okay

regards,

nazeer