C++ Nested Loops (with example programs)

- November 23, 2017
The loop inside the body of another loop is known as nested loop. The loop that contains another loop in its body is called outer loop. The loop used inside the body of outer loop is called inner loop.
The nesting can be done up to any level. However, as the level of nesting increases, the nested loop becomes more complicated. Any type of loop can be placed inside the body of any other loop. For example, a for loop can be placed inside the body of while loop.

Syntax of nested loop

The general syntax of nested loop is as follows:
outer_loop
{
   Statement (s) -1
   Inner_Loop
   {
       body of inner loop
   }
   Statement (s) -2
}
Where:
Outer_Loop It specifies the outer loop. It may be for while or do-while loop.
Inner_Loop It indicates the inner loop. It can be for, while or do-while loop.
Statement(s)-1 It specifies a statement or set of statements that is optional.
Statement(s)-2 It is also indicates a statement or set of statements that is optional.
 In the above nested loop syntax, Statement(s)-1, inner loop and statement(s)-2 represent the body of outer loop.

Flowchart of C++ Nested Loop

The general flowchart for the nested loop is shown in the following image.
Nested loop in C++

Working of Nested Loop 

The nested loop is executed by the following rules and regulations:
  1. First of all, the condition given in the outer loop is evaluated. If the condition is true, control enters inside the body of outer loop. If condition is false then loop is terminated.
  2. If the body of outer loop contains any statement before the inner loop, it is executed.
  3. The condition of inner loop is evaluated by the given below rules:-
    • If the condition is true, body of inner loop is executed for a specified numbers of times. If condition becomes false at the any stage then inner loop terminates and control transfers to the statement that comes after the body of inner loop.
    • If the condition is false, body of inner loop skipped and control goes to the statement that comes after the body of inner loop.
    • If there is any statement after the body of inner loop, it is executed and then control transfers to the beginning of outer loop.
  4. The steps from 1 to 3 are repeated until the condition given with outer loop remains true.
Look at the given program to understand it better:
#include <iostream>
using namespace std;
main();
{

  int u, i;
  u= 1;
  while (u<=3)
  {
     cout<<u<<endl;
     for (i = 1; i<= 2; i++)
     cout<<"I Love Programming\n";
     u = u+ 1;
   }
return 0;
}
In this program, the ‘while' loop structure contains the 'for' loop structure in its body. The ‘whi’le' loop structure is executed three times. Each time the 'while' loop structure is executed, the ‘for’ loop structure is executed two times. The output of the above program will be as under:
1
I Love Programming
I Love Programming
2
I Love Programming
I Love Programming
3
I Love Programming
I Love Programming

Nested Loop Example Program

This program displays the following output using the nested ‘for’ loop.
*
**
***
****
*****

#include <iostream>
using namespace std;
main ()
{
   int u, i;
for (u=1; u<=5; u++)
{
  for (i=1; i<=u; i++)
  cout<<"* ";
  cout<<endl;
}
return 0;
}