Thread: Nested Loops Help.

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    16

    Nested Loops Help.

    I was just learning about nested loops and got very confused about the example in Jumping in C++. Can you guys please help me through it and explain what each part of it does. Thanks! This is the code below:


    Code:
    #include <iostream>
    #include <string>
    
    
    usingnamespacestd;
    
    
    int main()
    
    
    {
        for(int i=0;i<10;i++)
        {
            cout<<'\t'<<i;
        }
    
        cout<<"\n";
    
        for(int i=0;i<10;++i)
        {
          cout<<i;
    
           for(int j=0;j<10;++j)
            {
               cout<<'\t'<<i*j;
        }
    
            cout<<"\n";
        }
    }
    
    

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent the code properly, e.g.,
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        for (int i = 0; i < 10; i++)
        {
            cout << '\t' << i;
        }
    
        cout << "\n";
    
        for (int i = 0; i < 10; ++i)
        {
            cout << i;
    
            for (int j = 0; j < 10; ++j)
            {
                cout << '\t' << i * j;
            }
    
            cout << "\n";
        }
    }
    As for what it does, start with the first loop, the one that isn't nested: what does it do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    16
    I don't know. Can you please explain what each part does.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you read and work through an introductory book on programming in C++. Accelerated C++: Practical Programming by Example by Koenig and Moo, although dated, remains a good book for this purpose.

    EDIT:
    Oh wait, you are working through an introductory book in C++. So, ignore my suggestion. Rather, go back to the part of the book that explains for loops and work through that part. Go back to the part of the book that explains printing to standard output with cout and operator<< and work through that part. It looks like you sped through to reach the part about nested loops before you have the fundamentals down.
    Last edited by laserlight; 03-27-2014 at 03:35 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loops
    By flysurfer in forum C Programming
    Replies: 5
    Last Post: 10-19-2012, 05:26 AM
  2. Nested Loops
    By wstayman in forum C Programming
    Replies: 3
    Last Post: 08-25-2012, 11:02 AM
  3. Nested loops..
    By her091 in forum C Programming
    Replies: 12
    Last Post: 06-17-2012, 02:22 PM
  4. nested for loops
    By akub3 in forum C Programming
    Replies: 2
    Last Post: 04-01-2004, 06:21 AM
  5. Nested for loops
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2002, 10:25 AM