Summary -

In this topic, we described about the below sections -

DO loop executes the block of statements until the specified number of times. DO loop is an unconditional looping statement.

No logical expression or condition used in DO loop. The block of statements can gets executed specified number of times in DO loop.

The block of statements is either a single statement or multiple statements. The block of statements coded in between DO and ENDDO statements.

ENDDO statement is mandatory with DO loop. All the statements should be ended with period (.) including DO and ENDDO.

Syntax -

DO [<n> TIMES] 
		[ VARYING <condition-variable> FROM <initial-value> 
		NEXT <increment-value>]. 
	.
	Statements-block-1.
	.
ENDDO.
Statements-block-2.

  • n - Specifies the number of times that the loop gets executed.
  • condition-variable - Specifies the variable that is used in the loop validation.
  • initial-value - Specifies the condition-variable initial value.
  • increment-value - Specifies the incrementing value for every loop.

Below diagram describes the flow of DO loop -

DO Loop Flow Diagram

The Statements-block1 gets executed n times. Once the loop iterates n times, loop gets terminated and control transferred to statements-block2.


Example -

Below example shows how the DO loop coded in the application program.


Code -

*&---------------------------------------------------------------------*
*& Report  Z_DO_LOOP
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_DO_LOOP.

* Declaring Variable
DATA W_I TYPE I VALUE 1.

* Executes DO loop for 10 times
DO 10 TIMES.

* Displaying Iteration number.
  WRITE : /'Iteration ',W_I.

* Adding 1 to iteration number.
  W_I = W_I + 1.

ENDDO.

Output -

DO Loop Example Output

Explaining Example -

In the above example, each and every statement is preceeded with a comment to explain about the statement. Go through them to get clear understanding of example code.

DO 10 TIMES... ENDDO, executes the loop blindly for 10 times without verifying a condition.

W_I = W_I + 1, adds 1 to W_I to display the iteration number, W_I = W_I + 1 statement is optional in the loop and doesn't have any impact on the loop iterations.