Problem Solving: Nested Loops
Learn about nested loops.
We'll cover the following
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:
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 (Isi <= 2
?). - If the condition is true, only then is the inner
for
loop control variable initialized (j = 1;
), and its condition expression is evaluated (Isj <= 2
?). If the innerfor
loop’s condition is true, then its body is executed (cout << i << " " << j << endl;
), and the value ofj
is updated as per the change factor step (j++
). Then the condition is checked again. If it is true, the innerfor
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 ofi
is updated as per the change factor step (i++
). - When the outer
for
loop condition becomes false, the outerfor
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:
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:
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 (Isi <= 2
?). - If the condition is true, only then isthe control variable initialized (
j = 1;
) and the innerwhile
loop condition expression is evaluated (Isj <= 2
?). If the innerwhile
loop’s condition is true, only then is its body executed (cout << i << " " << j << endl;
) and the value ofj
is updated as per the change factor step (j++
). After that, the condition is checked again. If it is true, the innerfor
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 ofi
is updated as per the change factor step (i++
). - When the outer
for
loop condition becomes false - the outerfor
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:
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:
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 (Isi <= 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 ofj
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 innerdo-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 ofi
is updated as per the change factor step (i++
). - When the outer
for
loop condition becomes false - the outerfor
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); } }
Let’s solve a quiz regarding nested loop:
Nested loops
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++;
}
1 1
2 1
1 2
2 2
1 1
1 2
2 1
2 2