cancel
Showing results for 
Search instead for 
Did you mean: 

system fields

what are the difference between sy-tabix and sy-index?

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

sy-tabix is for internal tasbles, which gives the number current row in procesing

sy-index id for loop processing, which gives the number of iterations done in a loop.

Former Member
0 Kudos

SY-TABIX

Current line of an internal table. SY-TABIX is set by the statements below, but only for index tables. The field is either not set or is set to 0 for hashed tables.

APPEND sets SY-TABIX to the index of the last line of the table, that is, it contains the overall number of entries in the table.

COLLECT sets SY-TABIX to the index of the existing or inserted line in the table. If the table has the type HASHED TABLE, SY-TABIX is set to 0.

LOOP AT sets SY-TABIX to the index of the current line at the beginning of each loop lass. At the end of the loop, SY-TABIX is reset to the value that it had before entering the loop. It is set to 0 if the table has the type HASHED TABLE.

READ TABLE sets SY-TABIX to the index of the table line read. If you use a binary search, and the system does not find a line, SY-TABIX contains the total number of lines, or one more than the total number of lines. SY-INDEX is undefined if a linear search fails to return an entry.

SEARCH <itab> FOR sets SY-TABIX to the index of the table line in which the search string is found.

SY-TFILL

After the statements DESCRIBE TABLE, LOOP AT, and READ TABLE, SY-TFILL contains the number of lines in the relevant internal table.

SY-TLENG

After the statements DESCRIBE TABLE, LOOP AT, and READ TABLE, SY-TLENG contains the length of the lines in the relevant internal table.

SY-TOCCU

After the statements DESCRIBE TABLE, LOOP AT, and READ TABLE, SY-TLENG contains the initial amount of memory allocated to the relevant internal table.

Loops

In a loop, a statement block is executed several times in succession. There are four kinds of loops in ABAP:

&#65399; Unconditional loops using the DO statement.

&#65399; Conditional loops using the WHILE statement.

&#65399; Loops through internal tables and extract datasets using the LOOP statement.

&#65399; Loops through datasets from database tables using the SELECT statement.

This section deals with DO and WHILE loops. SELECT is an Open SQL statement, and is described in the Open SQLsection. The LOOP statement is described in the sections on internal tables and extract datasets.

Unconditional Loops

To process a statement block several times unconditionally, use the following control structure:

DO [n TIMES] ...

[statement_block]

ENDDO.

Use the TIMES addition to restrict the number of loop passes to n.

If you do not specify any additions, the statement block is repeated until it reaches a termination statement such as EXIT or STOP (see below). The system field sy-index contains the number of loop passes, including the current loop pass.

You can nest DO loops and combine them with other loop forms.

Simple example of a DO loop:

DO.

WRITE sy-index.

IF sy-index = 3.

EXIT.

ENDIF.

ENDDO.

The list output is:

1 2 3

The loop is processed three times. Here, the processing passes through the loop three times and then leaves it after the EXIT statement.

Example of two nested loops with the TIMES addition:

DO 2 TIMES.

WRITE sy-index.

SKIP.

DO 3 TIMES.

WRITE sy-index.

ENDDO.

SKIP.

ENDDO.

The list output is:

1

1 2 3

2

1 2 3

The outer loop is processed twice. Each time the outer loop is processed, the inner loop is processed three times. Note that the system field sy-index contains the number of loop passes for each loop individually.

Conditional Loops

To repeat a statement block for as long as a certain condition is true, use the following control structure:

WHILE log_exp

[statemaent_block]

ENDWHILE.

log_exp can be any logical expression. The statement block between WHILE and ENDWHILE is repeated as long as the condition is true or until a termination statement such as EXIT or STOP occurs. The system field sy-index contains the number of loop passes, including the current loop pass.

You can nest WHILE loops to any depth, and combine them with other loop forms.

REPORT demo_flow_control_while.

DATA: length TYPE i VALUE 0,

strl TYPE i VALUE 0,

string(30) TYPE c VALUE 'Test String'.

strl = strlen( string ).

WHILE string NE space.

WRITE string(1).

length = sy-index.

SHIFT string.

ENDWHILE.

WRITE: / 'STRLEN: ', strl.

WRITE: / 'Length of string:', length.

The output appears as follows:

T e s t S t r i n g

STRLEN: 11

Length of String: 11

Here, a WHILE loop is used to determine the length of a character string. This is done by shifting the string one position to the left each time the loop is processed until it contains only blanks. This example has been chosen to demonstrate the WHILE statement. Of course, you can determine the length of the string far more easily and efficiently using the strlen function.

Terminating Loops

ABAP contains termination statements that allow you to terminate a loop prematurely. There are two categories of termination statement - those that only apply to the loop, and those that apply to the entire processing block in which the loop occurs. The STOPand REJECT statements belong to the latter group (see Exiting Eventblocks).

The termination statements that apply only to the loop in which they occur are CONTINUE, CHECKand EXIT. You can only use the CONTINUE statement in a loop. CHECK and EXIT, on the other hand, are context-sensitive. Within a loop, they only apply to the execution of the loop itself. Outside of a loop, they terminate the entire processing block in which they occur (subroutine, dialog module, event block, and so on).

CONTINUE, CHECK and EXITcan be used in all four loop types in ABAP (DO, WHILE, LOOP and SELECT).

Terminating a Loop Pass Unconditionally

To terminate a single loop pass immediately and unconditionally, use the CONTINUE statement in the statement block of the loop.

CONTINUE.

After the statement, the system ignores any remaining statements in the current statement block, and starts the next loop pass.

DO 4 TIMES.

IF sy-index = 2.

CONTINUE.

ENDIF.

WRITE sy-index.

ENDDO.

The list output is:

1 3 4

The second loop pass is terminated without the WRITE statement being processed.

Terminating a Loop Pass Conditionally

To terminate a single loop pass conditionally, use the CHECK condition statement in the statement block of the loop.

CHECK condition.

If the condition is not true, any remaining statements in the current statement block after the CHECK statement are ignored, and the next loop pass starts. condition can be any logical expression.

DO 4 TIMES.

CHECK sy-index BETWEEN 2 and 3.

WRITE sy-index.

ENDDO.

The list output is:

2 3

The first and fourth loop passes are terminated without the WRITE statement being processed, because sy-index is not between 2 and 3.

Exiting a Loop

To terminate an entire loop immediately and unconditionally, use the EXIT statement in the statement block of the loop.

EXIT.

After this statement, the loop is terminated, and processing resumes after the closing statement of the loop structure (ENDDO, ENDWHILE, ENDLOOP, ENDSELECT). In nested loops, only the current loop is terminated.

DO 4 TIMES.

IF sy-index = 3.

EXIT.

ENDIF.

WRITE sy-index.

ENDDO.

The list output is:

1 2

In the third loop pass, the loop is terminated before the WRITE statement is processed.