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

itab code

Former Member
0 Likes
1,044

hai,

Suppose itab is having 10 records, Write what will be the output of

Loop at itab.

if sy-tabix between 1 and 5.

write: /sy-index.

else if : sy-index between 6 and 10.

write: /sy-tabix.

Endif.

Endloop.

regards,

aravind.

8 REPLIES 8
Read only

Former Member
0 Likes
989

Isn't it going to write simply 1 to 10.

Regards,

Ravi

Note :Please mark all the helpful answers

Read only

former_member186741
Active Contributor
0 Likes
989

two different fields are being tested here. SY-TABIX holds the current iteration value within table itab. SY-INDEX could be anything depending on the preceding code, it is typically set by 'do' code rather than 'loop at tables'. Whatever it holds it will be constant in this loop.

SO, for the first 5 iterations SY-INDEX will be written, probaly this will hold '0' so there will be 5 0's listed.

For the next 5 iterations ONLY if SY-INDEX has a value between 6 and 10 will the SY-TABIX be written. So if sy-index is between 6 and 10 you'll see the TABIX written (which will also be 6 thru 10) but if sy-index does not hold 6 thru 10 NOTHING will appear.

ps, 'else if :' needs to be coded as 'elseif' or you'll get a syntax error.

Read only

Former Member
0 Likes
989

Hi Aravind,

The result would be every number between 1 and 10.

SY-TABIX is a system variable that stores the current position in the internal table.

Although there is difference between SY-TABIX and SY-INDEX, they are the same in this case.

- Priyanka Shukla

Please reward appropriate points.

Read only

Former Member
0 Likes
989

hi

As said by Neil this code will write only five 0s. For getting the correct value of sy-index you have to use this variable in do-enddo.

If you use the code within 'do - enddo' using

<i>read table itab index sy-index</i> and remaining as your code then you will get numbers 1 to 10 printed in a new line. also the write statement should be like <i>write : / sy-index</i>. there should be one space between '/' and 'sy-index'

regards

anoop

Read only

0 Likes
989

Hi Aravind,

The output of the code will be only 5 Zeroes that would be based on the sy-tabix variable.

sy-Index doesnot vary unless we explicitly increment or decrement the variable.

Hope it clarifies your doubt.

Best Regards,

Ram.

Read only

former_member184495
Active Contributor
0 Likes
989

hi aravind,

here sy-index would not come into picture since sy-index is for only looping statements like do...endo, while...endwhile,

whereas sy-tabix is used only for loop...endloop.

reward if worthful.

cheers,

Aditya.

Read only

Former Member
0 Likes
989

Hi Arvind

It will print just 5 0's at each line. Here value of sy-index will be 0 always because it is not inside any loop like do,while etc etc. But it is the internal table loop. So system field <b>SY-INDEX</b> value will remain 0 for the whole looping.

Read only

Former Member
0 Likes
989

Hi,

1)SY-INDEX has no effect in <b>LOOP....ENDLOOP</b>, only the SY-TABIX will hold the index of the record that is in the loop pass. So you the output will be all zeroes.

2)The reverse for <b>DO....ENDDO</b> only SY-INDEX will hold the current pass.

<b>Execute the Program to know the difference.</b>


REPORT zztest.


DATA : itab TYPE STANDARD TABLE OF mara WITH HEADER LINE.

START-OF-SELECTION.

  SELECT * FROM mara INTO TABLE itab UP TO 100 ROWS.

*---No effect on SY-INDEX


  WRITE : / 'SY-INDEX in LOOP...ENDLOOP'.
  LOOP AT itab.
    IF sy-tabix BETWEEN 1 AND 5.
      WRITE: / 'SY-INDEX', sy-index.
    ELSEIF sy-index BETWEEN 6 AND 10.
      WRITE: / 'SY-TABIX', sy-tabix.
    ENDIF.
  ENDLOOP.

*---No effect on SY-TABIX
  SKIP 1.
  WRITE : / 'SY-TABIX in DO...ENDO'.

  DO 10 TIMES.
    WRITE: / 'SY-INDEX', sy-index,
             'SY-TABIX', sy-tabix.
  ENDDO.

*---An Exception

  SKIP 1.
  WRITE : / 'An Exception'.
  DO 10 TIMES.

    LOOP AT itab.
      IF sy-tabix BETWEEN 1 AND 5.
        WRITE: / 'SY-INDEX', sy-index,
                 'SY-TABIX', sy-tabix.
      ENDIF.
    ENDLOOP.

  ENDDO.

<b>This is the Output</b>


<b>SY-INDEX in LOOP...ENDLOOP</b>
SY-INDEX          0
SY-INDEX          0
SY-INDEX          0
SY-INDEX          0
SY-INDEX          0

<b>SY-TABIX in DO...ENDO</b>
SY-INDEX          1  SY-TABIX          1
SY-INDEX          2  SY-TABIX          1
SY-INDEX          3  SY-TABIX          1
SY-INDEX          4  SY-TABIX          1
SY-INDEX          5  SY-TABIX          1
SY-INDEX          6  SY-TABIX          1
SY-INDEX          7  SY-TABIX          1
SY-INDEX          8  SY-TABIX          1
SY-INDEX          9  SY-TABIX          1
SY-INDEX         10  SY-TABIX          1

<b>An Exception</b>
SY-INDEX          1  SY-TABIX          1
SY-INDEX          1  SY-TABIX          2
SY-INDEX          1  SY-TABIX          3
SY-INDEX          1  SY-TABIX          4
SY-INDEX          1  SY-TABIX          5
SY-INDEX          2  SY-TABIX          1
SY-INDEX          2  SY-TABIX          2
SY-INDEX          2  SY-TABIX          3
SY-INDEX          2  SY-TABIX          4
SY-INDEX          2  SY-TABIX          5
SY-INDEX          3  SY-TABIX          1
SY-INDEX          3  SY-TABIX          2
SY-INDEX          3  SY-TABIX          3
SY-INDEX          3  SY-TABIX          4
SY-INDEX          3  SY-TABIX          5
SY-INDEX          4  SY-TABIX          1
SY-INDEX          4  SY-TABIX          2
SY-INDEX          4  SY-TABIX          3
SY-INDEX          4  SY-TABIX          4
SY-INDEX          4  SY-TABIX          5
SY-INDEX          5  SY-TABIX          1
SY-INDEX          5  SY-TABIX          2
SY-INDEX          5  SY-TABIX          3
SY-INDEX          5  SY-TABIX          4
SY-INDEX          5  SY-TABIX          5
SY-INDEX          6  SY-TABIX          1
SY-INDEX          6  SY-TABIX          2
SY-INDEX          6  SY-TABIX          3
SY-INDEX          6  SY-TABIX          4
SY-INDEX          6  SY-TABIX          5
SY-INDEX          7  SY-TABIX          1
SY-INDEX          7  SY-TABIX          2
SY-INDEX          7  SY-TABIX          3
SY-INDEX          7  SY-TABIX          4
SY-INDEX          7  SY-TABIX          5
SY-INDEX          8  SY-TABIX          1
SY-INDEX          8  SY-TABIX          2
SY-INDEX          8  SY-TABIX          3
SY-INDEX          8  SY-TABIX          4
SY-INDEX          8  SY-TABIX          5
SY-INDEX          9  SY-TABIX          1
SY-INDEX          9  SY-TABIX          2
SY-INDEX          9  SY-TABIX          3
SY-INDEX          9  SY-TABIX          4
SY-INDEX          9  SY-TABIX          5
SY-INDEX         10  SY-TABIX          1
SY-INDEX         10  SY-TABIX          2
SY-INDEX         10  SY-TABIX          3
SY-INDEX         10  SY-TABIX          4
SY-INDEX         10  SY-TABIX          5


Regards,

AS