Problem Solving: Nested Loops

Learn about nested loops.

Nested loops

In the previous lessons, we used loops and their nesting without specifically talking about them. Let us now discuss more deeply the syntax and working of the nested loops.

There are several versions of nested loops. Let’s discuss some of them.

for-for loop

If a for loop is written inside the body of another for loop, we will call it a nested for loop.

The syntax of nested for loop is is as follows:

Press + to interact
for (initialization; condition; increment)
{
for (initialization; condition; increment)
{
// Body statement of the inner loop
}
// Body statement of the outer loop
}

Go through these animated slides, to understand how nested loops get executed. Take a close look at the watches/local variables and how their values are changing while executing.

As can be seen in the animation above:

  • First, in the outer for loop, the control variable is initialized (i = 1;), and the condition expression is evaluated (Is i <= 2?).
  • If the condition is true, only then is the inner for loop control variable initialized (j = 1;), and its condition expression is evaluated (Is j <= 2?). If the inner for loop’s condition is true, then its body is executed (cout << i << " " << j << endl;), and the value of j is updated as per the change factor step (j++). Then the condition is checked again. If it is true, the inner for loop’s body is executed again, and this is repeated until the condition becomes false.
  • If the condition is false - the inner for loop breaks. The body of the outer loop is executed, and the value of i is updated as per the change factor step (i++).
  • When the outer for loop condition becomes false, the outer for loop breaks, and the control goes outside the loop.

Similarly, we can add the while loop inside the for loop.

for-while loop

If a while loop is written inside the body of a for loop, we will call it a nested for-while loop.

The syntax of a nested for-while loop is as follows:

Press + to interact
for (initialization; condition; increment)
{
while(condition)
{
// Body statement of the inner loop
}
// Body statement of the outer loop
}

Let’s write a code for the for-while loop:

Press + to interact
for(int i=1;i <= 2; i++)
{
int j=1;
while(j <= 2)
{
cout<<i<<" "<<j<<endl;
j++;
}
}

In a nested for-while loop:

  • First, in the outer for loop, the control variable is initialized (i = 1;) and its condition expression is evaluated (Is i <= 2?).
  • If the condition is true, only then isthe control variable initialized (j = 1;) and the inner while loop condition expression is evaluated (Is j <= 2?). If the inner while loop’s condition is true, only then is its body executed (cout << i << " " << j << endl;) and the value of j is updated as per the change factor step (j++). After that, the condition is checked again. If it is true, the inner for loop’s body is executed again, and this is repeated until the condition becomes false.
  • If the condition is false - the inner while loop breaks. Then the body of the outer loop is executed and the value of i is updated as per the change factor step (i++).
  • When the outer for loop condition becomes false - the outer for loop breaks, and the control goes outside the loop.

The for-do while loop

If a do while loop is written inside the body of a for loop, we will call it a nested for-do while loop.

Here is the syntax of the nested for-do while loop:

Press + to interact
for (initialization; condition; increment)
{
do
{
// Body statement of the inner loop
}
while(condition);
// Body statement of the outer loop
}

Let’s write an example of the for-do while loop:

Press + to interact
for(int i=1;i <= 2; i++)
{
int j=1;
do
{
cout<<i<<" "<<j<<endl;
j++;
}
while(j <= 2);
}

In a nested for-do while loop:

  • First, in the outer for loop, the control variable is initialized (i = 1;) and the condition expression is evaluated (Is i <= 2?).
  • If the condition is true, only then isthe control variable is initialized (j = 1;).
  • For the first time, the inner body of the do while loop inner body must be executed (cout << i << " " << j << endl;) and the value of j is updated as per the change factor step (j++).
  • Now its condition expression is evaluated (Is j <= 2?). If the condition is true, only then the inner do-while loop’s body is executed again, and this is repeated until the condition becomes false.
  • If the condition is false, the inner do while loop breaks. Then the body of the outer loop is executed, and the value of i is updated as per the change factor step (i++).
  • When the outer for loop condition becomes false - the outer for loop breaks, and the control goes outside the loop.

Let’s execute the following code (step-by-step execution) and see how every variable is changing after each execution.

#include <iostream>
using namespace std;

int main()
{
    for(int i=1;i <= 2; i++)
    {
        int j=1;
        while(j <= 2)
        {
            cout<<i<<" "<<j<<endl;
            j++;
        }
    }
    
    for(int i=1;i <= 2; i++)
    {
        int j=1;
        do
        {
            cout<<i<<" "<<j<<endl;
            j++;
        }
        while(j <= 2);
    }
    
}











Nested loops example

Let’s solve a quiz regarding nested loop:

Nested loops

1

What will be the output of the nested while-for loop below?

int i=1;
while(i <= 2)
{
    for(int j=1; j<=2; j++)
    {
        cout<<j<<" "<<i<<endl;
    }
    i++;
}
A)
1 1
2 1
1 2
2 2
B)
1 1
1 2
2 1
2 2
Question 1 of 40 attempted