Teknologi

LightBlog

Breaking

LightBlog

Wednesday, 29 August 2012

Looping


In a loop, a statement block is executed several times in succession. There are four kinds of loops in ABAP:
  • Unconditional loops using the DO statement.
  • Conditional loops using the WHILE statement.
  • Loops through internal tables and extract datasets using the LOOP statement.
  • Loops through datasets from database tables using the SELECT statement.

Unconditional Loops
To process a statement block several times unconditionally, use the following control structure:
DO [<n> TIMES] [VARYING <f> FROM <f1> NEXT <f 2>]. 
  <Statement block>
ENDDO.

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.Use the TIMES addition to restrict the number of loop passes to <n>.

Example:
DO 2 TIMES.
  WRITE SY-INDEX.
  SKIP.
  DO 3 TIMES.
    WRITE SY-INDEX.
  ENDDO.
  SKIP.
ENDDO.

The output is:
         1
         1          2          3
         2
         1          2          3


Conditional Loops
To repeat a statement block for as long as a certain condition is true, use the following control structure:
WHILE <condition> [VARY <f> FROM <f1> NEXT <f 2>]. 
   <statement block>
ENDWHILE.

<condition> can be any logical expressions. 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. 

Example:
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

Loops Statement
The LOOP and ENDLOOP statements define a loop around a statement block. The LOOP statement reads lines from internal table itab sequentially. You can either read all the lines or specify conditions cond to restrict which lines to read. The statement block between LOOP and ENDLOOP is executed once for each line.

LOOP AT itab result [cond]. 

  ... 
ENDLOOP. 

SELECT  END SELECT Statement

The result of the selection is meant to be a table, the data is usually  read line by line within a processing loop introduced by SELECT and concluded by ENDSELECT . For each line read, the processing passes through the loop once.SAP doesn't recommend this because of the performance issue.

If the result of the selection is meant to be a single record, the closing ENDSELECT is omitted.

Example:
SELECT SINGLE * FROM SFLIGHT
                WHERE
                  CARRID   = 'LH '      AND
                  CONNID   = '0400'     AND
                  FLDATE   = '19950228'.
SEATSFREE = SFLIGHT-SEATSMAX - SFLIGHT-SEATSOCC
END SELECT

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 termination statements that apply only to the loop in which they occur are CONTINUE, CHECK, and EXIT.

The statment 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).

Example to Terminating a loop pass unconditionally:
DO 4 TIMES.
  IF SY-INDEX = 2.
    CONTINUE.
  ENDIF.
  WRITE SY-INDEX.
ENDDO.

The output is:
         1          3          4
The second loop pass is terminated without the WRITE statement being processed.

Example to Terminating a loop pass conditionally:

DO 4 TIMES.
  CHECK SY-INDEX BETWEEN 2 and 3.
  WRITE SY-INDEX.
ENDDO.
The 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.


Example to Exiting a loop:

DO 4 TIMES.
  IF SY-INDEX = 3.
    EXIT.
  ENDIF.
  WRITE SY-INDEX.
ENDDO.

The output is:
         1          2
In the third loop pass, the loop is terminated before the WRITE statement is processed.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Adbox