I know it is very wide question. We all use System fields like SY-SUBRC to check last statement was successful or not, SY-DBCNT for Database access count, SY-INDEX for row, SY-TABIX for current processed row in Loops, etc.
Check out this code.
System Field behavior
TYPES: BEGIN OF lty_data, num TYPE i, fld2 TYPE i, END OF lty_data. DATA: li_data TYPE STANDARD TABLE OF lty_data. DATA: lwa_data LIKE LINE OF li_data. * 5 entries in table DO 5 TIMES. lwa_data-num = sy-index * 2. APPEND lwa_data TO li_data. ENDDO. DO 5 TIMES. READ TABLE li_data INTO lwa_data WITH KEY num = sy-index. IF sy-subrc EQ 0. WRITE: /(12) 'Entry Found,', sy-index, 'subrc', sy-subrc, 'tabix', sy-tabix. lwa_data-fld2 = sy-index * 2. ELSE. WRITE: /(12) 'Not Found,', sy-index, 'subrc', sy-subrc, 'tabix', sy-tabix. ENDIF. MODIFY li_data FROM lwa_data INDEX sy-tabix. ENDDO.
At first it looks ok except the Modify statement outside the IF..ENDIF. Since the MODIFY statement is outside the IF, it would try to update the entry even when it is not found (SY-SUBRC = 4). MODIFY statement is using the SY-TABIX to update the entry in the ITAB. If you run this code it would end up in short-dump.
Why it ended in Short-Dump?
The reason is behind the SY-TABIX value. Whenever SY-SUBRC gets set to 4, SY-TABIX gets cleared – means set to 0. In other words, system make the TABIX to zero as it couldn’t found the entry (SUBRC = 0). Since it would try to update the table with zero index, it would end up in short dump. To check the content of the SY-TABIX, comment out the MODIFY statement and try the program again. You would see the output similar to this.
In above sample code, it is required to move the MODIFY statement to avoid the short dump withing the IF .. ELSE part.
If you need to use the TABIX value for any other purpose within the LOOP, it would be a better idea to save that into a local variable and use it for the purpose. Something similar to this would help you to keep SY-TABIX value even after any other failed READ.
LOOP AT li_vbak INTO lwa_data. v_tabix = sy-tabix. READ TABLE li_data_2 INTO lwa_data_2 WITH KEY fld = lwa_data-fld2. IF sy-subrc = 0. ENDIF. * by the time it reaches here, SY-TABIX would be initial * so, use to V_TABIX if it is required to access TABIX again. ENDLOOP.