Summary -

In this topic, we described about the below sections -

A loop in the another loop is called as nested Loop. These loops can contains same loops as nested or different loops as nested together.

A WHILE can contain WHILE or DO loop as nested and similarly DO loop contains WHILE or DO loop as nested. For specific loop statement, the corresponding scope terminator is mandatory. Each statement in the nested loop should end with a period (.) including loop statements.

Syntax - WHILE Nested

WHILE <logical-expression/Condition>.
	Statements-block-1.
	WHILE <logical-expression/condition>.
		Statements-block-2.
	ENDWHILE.
ENDWHILE.
Statements-block-3.

Syntax - DO Nested

DO [n times].
	Statements-block-1.
	DO [m times].
		Statements-block-2.
	ENDDO.
ENDDO.
Statements-block-3.

Syntax - WHILE Nested with DO

WHILE <logical-expression/Condition>.
	Statements-block-1.
	DO [m times].
		Statements-block-2.
	ENDDO.
ENDWHILE.
Statements-block-3.

Syntax - DO Nested with WHILE

DO [n times].
	Statements-block-1.
	WHILE <logical-expression/condition>.
		Statements-block-2.
	ENDWHILE.
ENDDO.
Statements-block-3.

Below diagram describes the flowchart of Nested DO loop -

Nested DO Loop Flow Diagram

The Statements-block1 executes n times. For each iteration of n, there is DO loop with m iterations. For each iteration of first DO loop, second loop executes m times. i.e. Statements-block2 gets executed m times. Once the second DO loop iterates m times, then second DO loop gets terminated and control transfers to the first DO loop for another iteration.

Once the first DO loop iterates n times, loop gets terminated and control transferred to statements-block3.


Example -

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


Code -

*&---------------------------------------------------------------------*
*& Report  Z_NESTED_DO
*&---------------------------------------------------------------------*
*& Written by TutorialsCampus
*&---------------------------------------------------------------------*

REPORT  Z_NESTED_DO.

* Declaring Variable
DATA: W_I TYPE I VALUE 1,
      W_J TYPE I.

* Iterating loop 3 times
DO 3 TIMES.

* Displaying first loop number
  WRITE : /'Outer loop iteration ',W_I.

* Adding loop number
  W_I = W_I + 1.

* Initializing inner loop number
  W_J = 1.

* Iterating loop 5 times.
  DO 5 TIMES.

* Displaying second loop number
    WRITE : /'       Inner loop iteration ',W_J.

* Adding 1 to inner loop number
    W_J = W_J + 1.

  ENDDO.

ENDDO.

Output -

Nested 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 3 TIMES.. ENDDO, iterates the set of statements in the loop 3 times unconditionally. That is DO 5 TIMES.. ENDDO loop also gets executed 3 times. W_I = W_I + 1, increments the number by 1 for every iteration and has no impact on iterations.

W_J = 1 used to initilaize the inner loop iteration for every upper loop iteration.

DO 5 TIMES.. ENDDO, iterates the set of statements in the loop 5 times unconditionally. W_J = W_J + 1, increments the number by 1 for every iteration and has no impact on iterations.

W_I = W_I + 1 and W_J = W_J + 1, doesn't have an impact on loops iteration and used only for displaying iterating loop number.