# for loop confusion

• 04-25-2006
Enges
for loop confusion
Ok, I'm just learning C++ by use of a book called You Can Do It, A Beginners Intro to Comp Programming. I'm on Chapter 2, and have just learned the for-loop. At the end, there are these exercises to do. The last one requires you to display all 256 colors on an object call paper that is specific to the compiler that came with the book.

Now, the book provided the following code for the exercise:

Code:

```        for(int i(0); i != 16; ++i){                 for(int c(0); c != 16; ++c){                                         paper.plot(c - 8, i - 8, (i  * 16 + c));                                                                                         }                        }```
It works, displaying all of the colors on the paper object. The confusion is how it works. Looking at it, I don't understand how this would ever plot all 256 colors in a perfect sqaure on the screen.

Can somebody help me out? And feel free to ask questions if you don't understand what the code is about or what I'm asking or anything else.
• 04-25-2006
Prelude
>You Can Do It, A Beginners Intro to Comp Programming
Not a bad choice, Francis is fairly well respected in the C++ community. I personally would recommend Accelerated C++ instead, but some people would rather have graphics. ;)

>I don't understand how this would ever plot all 256 colors in a perfect sqaure on the screen.
Let's take a look. There's nothing immediately obvious that shows how the colors will print, so you have to assume that it's magic by the plot member function.

>paper.plot(c - 8, i - 8, (i * 16 + c));
Page 20 tells you how the plot function works. Presumably m is equivalent to x and n is equivalent to y in the usual coordinates convention. So it makes sense to guess that the first argument (c - 8) handles the column and the second argument (i - 8) handles the row, and both are completely dependent on the value of the loop counter. In the end it's a math trick. :)
• 04-25-2006
eletron
Is 2 loops c inside i, so 16*16=256
• 04-25-2006
Enges
Yes, but going through the loop step by step, I would think it would produce a diagonal line. Because each time, both m and n are increasing at the same time, and then the color is plotted.
• 04-25-2006
Prelude
>Because each time, both m and n are increasing at the same time
Not exactly. For each iteration of the outer loop, the inner loop runs completely:
Code:

```#include <iostream> using namespace std; int main() {   for ( int i = 0; i < 10; i++ ) {     for ( int j = 0; j < 10; j++ )       cout<<'('<< i <<','<< j <<") ";     cout<<'\n';   } }```
The effect of your program is identical except instead of text index pairs, you print colors.
• 04-25-2006
whiteflags
No, electron is right, 16 is squared so you get a square. Yay.
• 04-26-2006
Bench82
Quote:

Originally Posted by Enges
Yes, but going through the loop step by step, I would think it would produce a diagonal line. Because each time, both m and n are increasing at the same time, and then the color is plotted.

No, the two numbers aren't increasing at the same time :) The number for the outer-loop only increases when the inner-loop has completely finished (in other words, the inner loop has run 16 times).
Once the outer-loop increments, the inner-loop runs fully all over again.. another 16 times.. then the outer-loop increments again.. etc. etc.

EDIT : duh, that's just what prelude said.. ignore that ;)