‎2009 Sep 25 3:50 PM
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.
‎2009 Sep 25 4:13 PM
> 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
‎2009 Sep 25 3:58 PM
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
‎2009 Sep 25 3:58 PM
‎2009 Sep 25 4:13 PM
> 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
‎2009 Sep 25 4:23 PM
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.
‎2009 Sep 25 4:24 PM
‎2009 Sep 25 4:26 PM
‎2009 Sep 25 4:33 PM
Yet another reason not to change a table that you are looping through.
Rob