Jump to content

  • Log In with Google      Sign In   
  • Create Account


A little confused with nested for loops


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 SaintForHire   Members   -  Reputation: 115

Like
0Likes
Like

Posted 09 July 2013 - 06:35 PM

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;
    }
}


Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6974

Like
3Likes
Like

Posted 09 July 2013 - 06:50 PM

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?


[ 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 ]

#3 Paradigm Shifter   Crossbones+   -  Reputation: 5214

Like
3Likes
Like

Posted 09 July 2013 - 06:52 PM

 

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

#4 SaintForHire   Members   -  Reputation: 115

Like
0Likes
Like

Posted 09 July 2013 - 07:03 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS