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

Where condition in Loop statement

Former Member
0 Likes
6,200

Hi All,

I came across a weird problem in a where condtion in loop statment. Please try the below code in your system:

REPORT ztest_report .

TYPES: BEGIN OF t_data,

deal_no TYPE int4,

count TYPE int4,

index TYPE n,

END OF t_data.

DATA: int_data TYPE STANDARD TABLE OF t_data,

wa_data TYPE t_data,

lv_diff TYPE int4 VALUE '1090',

v_job_index TYPE i VALUE '111'.

wa_data-deal_no = 1.

wa_data-count = 655.

APPEND wa_data TO int_data.

wa_data-deal_no = 1.

wa_data-count = 656.

APPEND wa_data TO int_data.

LOOP AT int_data INTO wa_data WHERE index IS INITIAL

AND count <= lv_diff.

lv_diff = lv_diff - wa_data-count.

wa_data-index = v_job_index.

MODIFY int_data FROM wa_data TRANSPORTING index.

WRITE:/ 'modified'.

ENDLOOP.

The expected output is, 'modified' written in the output screen once. But it writes twice because I can see in debugging mode that control goes into the loop second time also (when lv_diff is 453 which is not greater than 656).

I tried giving a check condition inside the loop and it worked fine. I changed the value of lv_diff to 600, in that case it doesn't go into the loop itself.

Can anybody explain me what is the real problem with this logic?

Also would like to know if there is any rule that says this type of conditions should not be used within the loop. I'm having an impression that, it should work with the where condition.

Any pointers on this is very much appreciated.

Thanks for your help.

1 ACCEPTED SOLUTION
Read only

ThomasZloch
Active Contributor
0 Likes
2,453

> The logical expression declared after WHERE is evaluated once when entering the loop. Any changes to the second operand during loop processing are ignored

http://help.sap.com/abapdocu_70/en/ABAPLOOP_AT_ITAB_COND.htm#!ABAP_ADDITION_3@3@

Thomas

7 REPLIES 7
Read only

Former Member
0 Likes
2,453

Hi ,

try this way....



FIELD-SYMBOLS: <fs_stru> TYPE ANY.   "declare this

TYPES: BEGIN OF t_data,
deal_no TYPE int4,
count TYPE int4,
index TYPE n,
END OF t_data.


DATA: int_data TYPE STANDARD TABLE OF t_data,
wa_data TYPE t_data,
lv_diff TYPE int4 VALUE '1090',
v_job_index TYPE i VALUE '111'.



wa_data-deal_no = 1.
wa_data-count = 655.
APPEND wa_data TO int_data.

wa_data-deal_no = 1.
wa_data-count = 656.
APPEND wa_data TO int_data.

"new loop
LOOP AT int_data INTO <fs_stru> WHERE index IS INITIAL
AND count <= lv_diff.
lv_diff = lv_diff - <fs_stru>-count.
<fs_stru>-index = v_job_index.
ENDLOOP.


Prabhudas

Read only

Former Member
0 Likes
2,453

edited

Edited by: Prabhu Das on Sep 25, 2009 8:29 PM

Read only

ThomasZloch
Active Contributor
0 Likes
2,454

> The logical expression declared after WHERE is evaluated once when entering the loop. Any changes to the second operand during loop processing are ignored

http://help.sap.com/abapdocu_70/en/ABAPLOOP_AT_ITAB_COND.htm#!ABAP_ADDITION_3@3@

Thomas

Read only

Former Member
0 Likes
2,453

From ABAP help, you can see that the loop's WHERE condition is executed at the entry into the loop not effected by subsequent changes.

Notes

The logical expression specified after WHERE is analyzed once at entry into the loop. Possible changes of the second operand during loop processing are not taken into account.

Read only

0 Likes
2,453

oops Thomas beat me to it.

Read only

0 Likes
2,453

Confirms that we have a point

Good weekend

Thomas

Read only

Former Member
0 Likes
2,453

Yet another reason not to change a table that you are looping through.

Rob