Today, I encountered a strange problem when I debugged the program for more than an hour and found that it was such an error. This problem is hard to be found, so I wrote it for your reference.
Do not write the ABAP program like this. Otherwise, you may not even know how to die:
I modified the program in the project and extracted the following paragraph:
Report z_test_loop.
Do 10 times.
Data: lv_bool type char1 value space.
If sy-Index = 5.
Lv_bool = 'x '.
Endif.
If lv_bool = 'x '.
Write 'true '.
Else.
Write 'false '.
Endif.
New-line.
Enddo.
The Code logic is very simple, that is, to make a loop, a total of 10 times, when the fifth time will be marked as true, it is reasonable to say that each cycle at the beginning will call data: lv_bool type char1 value space. therefore, if this parameter is not set, it is set to false.
The problem arises. Starting from 5th, all lv_bool values are true and cannot be changed again.
In the past, the variable scope of the ABAP program was a method or function module, rather than a block in our general concept. Therefore, when the system is in a loop, because the system variables have been defined, the defined field is no longer executed. The default value is the value obtained in the previous loop, which leads to the above problem. Therefore, the definition must be written as follows:
Data: lv_bool type char1.
Lv_bool = space.
If this program appears in a large program, it is still difficult to adjust. This article is only for your reference by abaper.