A little confused with nested for loops

Started by
2 comments, last by SaintForHire 10 years, 9 months ago

Hello. Kind of new to gamedev.net and to programming in general. Hopefully this is the right place to post.

I finished this code below and got it to work. The first nested for loop outputs + signs on each line. 1 on the first line, all the way to 10 on the 10th line.

The second nested for loop does the same just reverse.

My question is, why doesn't the second nested for loop terminate when j is no longer >= i?


#include <iostream>
 
using namespace std;
 
int main()
{
    for (int i = 1; i <= 10; ++i)
    {
        for (int j = 1; j <= i; ++j)
        {
            cout << "+";
        }
        cout << endl;
    }
    cout << endl;
 
    for (int i = 1; i <= 10; ++i)
    {
        for (int j = 10; j >= i; --j)
        {
            cout << "+";
        }
        cout << endl;
    }
}
Advertisement

My question is, why doesn't the second nested for loop terminate when j is no longer >= i?

But it does terminate when j is no longer >= i... Can you clarify your question?

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

My question is, why doesn't the second nested for loop terminate when j is no longer >= i?

But it does terminate when j is not longer >= i... Can you clarify your question?

^^ What he said.

Try doing

cout << "(" << i << ", " << j << ").";

instead of cout << "+";

And you will be able to see the values of i and j on the screen each iteration.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Oh wow, I get it now. It's hard to explain out loud how I was thinking of it before. It was just weird for me I guess.

Thank both of you very much.

This topic is closed to new replies.

Advertisement