Can someone explain nested loops to me(loop in loop)?

I'm reading "Jumping into C++" and I've read the explanation for a nested loop, but the example code which is a multiplication table makes it hard to understand so I was wondering if someone could better explain it with their own code. It doesn't need to be extremely long code but just to get a good example of how to use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;


int main ()
{
    for ( int i = 0; i < 10; i++ )
    {
        cout << '\t' << i; // \t represents a tab character, which will
        //formatour output nicely
    }
    cout << '\n';
    for ( int i = 0; i < 10; i++ )
    {
        cout << i;
        for ( int j = 0; j < 10; j++)
        { cout<<'\t'<<i*j;
        }cout << '\n'; }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
 for ( int i = 0; i < 10; i++ )
{
    cout << "Outer Loop\n";

    for ( int j = 0; j < 10; j++)
    { 
        cout << "Inner Loop\n";
    }

    cout << '\n'; 
}


In this specific case, for every one outer loop run, the inner loop will run 10 times.
A nested loop is just a loop that is inside of another loop.

If you understand a single loop, it shouldn't be hard to understand a nested loop.

The multiplication table example is pretty good. You have one loop (the outer loop... the 'i' loop) which loops 10 times, one for each row:

1
2
3
4
5
6
for(int row = 0; row < 10; ++row)
{
    // stuff here is done once for each of the 10 rows

    cout << '\n';  // print a new line after each row
}



Now disregarding that loop for a second... let's make another loop that also loops 10 times... this time once for each column:

1
2
3
4
5
6
for(int col = 0; col < 10; ++col)
{
    // stuff here is done once for each of the 10 columns

    cout << '\t';  // print a tab after each column
}



Both of these loops are simple enough on their own, right?

So a nested loop just combines them. Since each row will consist of several columns we simply "loop the column loop" by putting the column loop inside the row loop:

1
2
3
4
5
6
7
8
9
10
11
for(int row = 0; row < 10; ++row)
{
    for(int col = 0; col < 10; ++col) // <- in each row, loop for each colum
    {
        // stuff here is done once for each column (in each row)

        cout << '\t';  // print a tab after each column (in each row)
    }

    cout << '\n';  // print a new line after each row
}



The multiplication table can then easily be calculated by multiplying the current row * current column:

1
2
3
4
5
6
7
8
9
10
11
for(int row = 0; row < 10; ++row)
{
    for(int col = 0; col < 10; ++col) // <- in each row, loop for each colum
    {
        cout << (row * col);

        cout << '\t';  // print a tab after each column (in each row)
    }

    cout << '\n';  // print a new line after each row
}
I'm reading "Jumping into C++" and I've read the explanation for a nested loop, but the example code which is a multiplication table makes it hard to understand so I was wondering if someone could better explain it with their own code. It doesn't need to be extremely long code but just to get a good example of how to use it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;


int main ()
{
    for ( int i = 0; i < 10; i++ )
    {
        cout << '\t' << i; // \t represents a tab character, which will
        //formatour output nicely
    }
    cout << '\n';
    for ( int i = 0; i < 10; i++ )
    {
        cout << i;
        for ( int j = 0; j < 10; j++)
        { cout<<'\t'<<i*j;
        }cout << '\n'; }
}

Ok first for loop(declares integer i(i = 0))
second loop(declares integer j(j = 0))

first loop i++(i=1)
second loop j++(j=1) prints out first row first column
j=2 prints out first row second column
...
...
...
after j < 10
i++ i = 2
j++(j=1)
j++(j=2)
...
...
...

so every time your first loop runs your nested loop completes all of its loops
then you first loop runs again nested loop does full loop(in this case j <10)

it does this until you first loop has finished(i < 10)
Last edited on
Topic archived. No new replies allowed.