C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Wednesday, December 18, 2013

Explain Working of Nested For Loop With Example Code

What is a Nested For Loop?

A nested for loop is a for loop within another for loop. Consider the following example code:

     for ( i = 1; i <= 2; i++)
               for ( k = 1; k<= 3; k++)
                     cout<<"Easy C++ Programming"<<endl;

Here, the C++ statement ' cout<<"Easy C++ Programming"<<endl; ' will be executed 2 x 3 = 6 times.
So output of the above C++ code will be:

Easy C++ Programming
Easy C++ Programming
Easy C++ Programming
Easy C++ Programming
Easy C++ Programming
Easy C++ Programming
What is nested for loop, working of nested for loop In above figure, it is obvious that a single iteration of Outer Loop(for value of i = 1), Inner Loop will perform 3 repetitions for value of k=1, k=2 and k=3.
Similarly, it is obvious that a second iteration of Outer Loop(for value of i = 2), Inner Loop will perform 3 repetitions for value of k=1, k=2 and k=3.

 How Nested For Loop Works?


First of all, the variable (loop counter) i is initialized to 1.
Loop condition i<=2 is checked for i = 1, which gives TRUE.
So the control goes to next for loop with loop counter k.

k is initiaized to 1.
Loop condition k<=3 is checked for k=1, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for first time.

k is incremented by 1.  So now value of k is 2.
Loop condition k<=3 is checked for k=2, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for second time.

k is incremented by 1.  So now value of k is 3.
Loop condition k<=3 is checked for k=3, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for third time.

k is incremented by 1.  So now value of k is 4.
Loop condition k<=3 is checked for k=4, which gives FALSE.
So loop is terminated.

The Control will go to  i++ of first for loop, so i is incremented by 1. Now value of i is 2.
Loop condition i<=2 is checked for i = 2, which gives TRUE.
So the control goes to next for loop with loop counter k.

k is initiaized to 1.
Loop condition k<=3 is checked for k=1, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for fourth time.

k is incremented by 1.  So now value of k is 2.
Loop condition k<=3 is checked for k=2, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for fifth time.

k is incremented by 1.  So now value of k is 3.
Loop condition k<=3 is checked for k=3, which gives TRUE.
So loop body is executed. Easy C++ Programming is printed for sixth time.

k is incremented by 1.  So now value of k is 4.
Loop condition k<=3 is checked for k=4, which gives FALSE.
So loop is terminated.

The Control will go to  i++ of first for loop, so i is incremented by 1. Now value of i is 3.
Loop condition i<=2 is checked for i = 3, which gives FALSE.
So the Loop is terminated.

This completes the execution of the nested for loop. So the statement '  cout<<"Easy C++ Programming"<<endl; ' will be executed for 6 times.

Share:

0 comments:

Post a Comment

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Blog Archive

Labels