for loop inside for loop

Aug 2, 2012 at 5:29pm
Hello everyone!
I am a beginner in C + + and I have a question about for loops. I understand the global concept of a for loop, but I do not know how it works in a situation where there are multiple for loops inside a for loop :D
I hope I was precise enough, and if there is someone who can help me I would be very thankfull. I need someone who can explain it to me as explaining to a little child. I feel that there is no progress for me if I don't understand this. (btw, I have read hundreds of articles on the internet and read a dozen books, but there is not well explained this segment) :/
Aug 2, 2012 at 5:38pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
   for (int i = 0; i < 10; i++)
   {
        std::cout << "i = " << i << std::endl;
        for (int c = 0; c < 20; c++)
        {
             std::cout << "c = " << c << std::endl;
        }
    }
    return 0;
}


I haven't compiled it but it should work

output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
i = 0
c = 0
c = 1
...
c = 19
i = 1
c = 0
c = 1
...
c = 19
i = 2
...
i = 9
c = 0
c = 1
...
c = 19


code goes from top to bottom, if a for loop encounters another for loop it will do that for loop until it finishes before ending the first round of the for loop that it was in.
Last edited on Aug 2, 2012 at 5:41pm
Aug 2, 2012 at 6:10pm
Let's say that I understand this. But what if I have a situation where I have 3 for loops, one inside the other?
How is the program executed then?
Last edited on Aug 2, 2012 at 6:10pm
Aug 2, 2012 at 6:15pm
just trace it out with something small like
1
2
3
4
5
6
7
8
for(int i=0; i<2; i++)
   for(int j=0; j<2; j++)
       for(int k=0; k<2; k++)
       {
             cout<<"i: "<<i<<endl;
             cout<<"j: "<<j<<endl;
             cout<<"k: "<<k<<endl;
        }

and see what happens...
Aug 2, 2012 at 6:31pm
I guess this last program counts binary numbers till 8 ( 000 --> 111 ).
I really want to understand this last program ... but it's complicated to me ... I don't know ... maybe I'm really stupid or retarded to understand this xD
The first program was easy to understand ... but the last one is hard.
Sorry people :(
Aug 2, 2012 at 6:47pm
The concept is simple:

0. The initialization part is executed (e.g. int i=0).
1. the condition is checked.
2. if the condition is true, the body and increment statement (e.g. i++) is executed. - then go back to 1.
3. if it is false, the loop ends.

When the body happens to contain a for loop, see 0.

Change it to the following to better see what's going on:
1
2
3
4
for(int i=0; i<2; i++)
   for(int j=0; j<2; j++)
       for(int k=0; k<2; k++)
          cout << "i: "<< i << "  j: " << j << "  k: " << k << endl;
Last edited on Aug 2, 2012 at 6:52pm
Aug 2, 2012 at 6:55pm
I understand the whole concept of for loops. My biggest problem are multiple for loops (3 or more), especially when they are one inside the other. :/
So far, I'm slowly beginning to realizing this.
Aug 2, 2012 at 7:06pm
Well, the point is that the concept doesn't change just because the loops are nested.
You just need to replay in your mind step by step what's happening.
Aug 2, 2012 at 7:08pm
<note> This is not a program, it is an analogy so don't look too far into it.

Imagine nested for loops like the earth, moon and a satellite going around the moon.
The earth orbits the sun as the moon orbits the earth as the satellite orbits the moon.
1
2
3
4
5
6
7
8
for(int i=0; i<100; i++) ///this could be our earth
   for(int j=0; j<100; j++)  ///this could be our moon
       for(int k=0; k<100; k++)  /// this could be our satellite
       {
             cout<<"i: "<<i<<endl;
             cout<<"j: "<<j<<endl;
             cout<<"k: "<<k<<endl;
        }

The satellite travels fastest around the moon
the moon takes a longer amount of time
and the earth takes the slowest, 365 days.

So the inner portion will happen the more often than the previous and that one will take iterate more often than the first for loop...

Normally we use nested for-loops to iterate arrays so like:

int square[10][10];
//this is a square with a width(x) of 10, height(y) of 10
//the int at each index could represent the color at each point


1
2
3
4
5
 for(int x=0; x<10; x++)
       for(int y=0; y<10; y++)
       {
             square [x][y] = randomColorGenerator();
        }

This loop would set the square 's color one vertical stripe at a time... basically like:
| | | |
VVVV.............. Until the entire square is colored
Last edited on Aug 2, 2012 at 7:09pm
Aug 2, 2012 at 7:11pm
I know and that's the hardest thing :D
Especially for me because I'm a visual type of a person. A good step-by-step animation would help, similar to this when I was looking how bubble sort in working:
http://en.wikipedia.org/wiki/File:Bubble-sort-example-300px.gif

Any animation maybe?
Aug 2, 2012 at 7:19pm
Common Wealth thank you! Now it's all clear!
Good explained!
Aug 2, 2012 at 7:26pm
But....why is it starting to print in j, k, i order instead of i, j, k?

j: 0
k: 0
i: 9
...
j: 0
k: 1
i: 9
...
j: 0
k: 2
i: 9
Aug 2, 2012 at 7:29pm
But....why is it starting to print in j, k, i order instead of i, j, k?

It doesn't. Print all values on the same line so you can't fall victim to that illusion.
Last edited on Aug 2, 2012 at 7:30pm
Aug 2, 2012 at 7:41pm
Ok...I fixed the code like this...

1
2
3
{
             cout << "i: " << i << " j: " << j << " k: " << k << endl;
        }


...and compiled and I'm even more confused with the output:

i: 7 j: 0 k: 1
i: 7 j: 0 k: 2
i: 7 j: 0 k: 3
i: 7 j: 0 k: 4
i: 7 j: 0 k: 5
i: 7 j: 0 k: 6
i: 7 j: 0 k: 7
i: 7 j: 0 k: 8
i: 7 j: 0 k: 9
i: 7 j: 1 k: 0
i: 7 j: 1 k: 1
i: 7 j: 1 k: 2
i: 7 j: 1 k: 3
...

Is it normal that it starts from i: 7 or am I doing something wrong?
Last edited on Aug 2, 2012 at 7:42pm
Aug 2, 2012 at 7:48pm
Is it normal that it starts from i: 7 or am I doing something wrong?

You probably set your terminal's line buffer size to a value that is too small the hold all of the output. Change the setting, redirect the output to a file, or just limit the number of iterations per loop to 2 or 3.
Aug 2, 2012 at 9:25pm
If you don't get what Athar's saying, it's that your console can't keep all the lines as it writes so it just clears the ones that are 'old' and puts in the next ones. So basically you're looking at somewhere in the middle of the printing

Aug 3, 2012 at 12:18pm
Now it's ok. Thanks people! :)
Topic archived. No new replies allowed.