Thread: newbie request..(Please)

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    17

    newbie request..(Please)

    Hi all
    I do understand nested for-loop when two for-loop is used but i get puzzled in case of three or more loops. I have surfed that on the internet but as a beginner in c++ i can't understand this, will someone explain it to me that how more than two For loop works.

    Please give a descriptive explanation.

    Thanking all in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you understand a nested for loop, then increasing the number of nesting levels should not pose a problem to you in itself.

    So, come up with an example of nested loops, and explain it. I'll take your explanation and extend it to yet another nesting level
    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 rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You can explain this to yourself without a computer, use a pen and paper, or use three coins (big , medium, small will help to visualise) and draw three rows numbered 0 to 9 on a notepad, put the biggest coin on the 0 on top row, the medium coin on 0 on middle row and small one on 0 on bottom row, now move the counters according to how you think the loop works, and it will quickly come to you...if you can understand the descritption of my experiment that is.. ;->
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Hello Aash
    Here is one way to visualize what goes on as the for loops increase in number

    1 For Loop
    Code:
    for(int a = 0 ; a <= 1 ; a++) //from 0 to 1
        cout << a << endl; //output current value of a with new line
    This would give
    0
    1

    2 For Loops
    Code:
    for(int a = 0 ; a <= 1 ; a++) //from 0 to 1
        for(int b = 0 ; b <= 1 ; b++) //from 0 to 1
            cout << a << b << endl; //output a,b and end line
    This would give
    00
    01
    10
    11

    3 For Loops
    Code:
    for(int a = 0 ; a <= 1 ; a++) //from 0 to 1
        for(int b = 0 ; b <= 1 ; b++) //from 0 to 1
            for(int c = 0 ; c <= 1 ; c++) //from 0 to 1
                cout << a << b << c << endl; //output a,b,c and end line
    This would give
    000
    001
    010
    011
    100
    101
    110
    111


    As you can see, a becomes longer and longer since it has to accommodate the For Loops within its own loop.
    The range of a column (top-down) is independent from another.
    You can see this by doing

    3 For Loops
    Code:
        for(int a = 0 ; a <= 1 ; a++) //from 0 to 1
            for(int b = 0 ; b <= 2 ; b++) //from 0 to 2
                for(int c = 0 ; c <= 1 ; c++) //from 0 to 1
                    cout << a << b << c << endl; //output a,b,c and end line
    a b c
    0 0 0
    0 0 1
    0 1 0
    0 1 1
    0 2 0
    0 2 1
    1 0 0
    1 0 1
    1 1 0
    1 1 1
    1 2 0
    1 2 1

    b's range is from 0 to 2, in this case.


    Good luck
    Last edited by CPlus; 08-07-2010 at 10:34 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    17
    Thanks laserlight, rogster001

    @CPlus thankyou for your nice effort, it's valuable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-18-2010, 04:14 AM
  2. Function call from another .c module
    By Ali.B in forum C Programming
    Replies: 14
    Last Post: 08-03-2009, 11:45 AM
  3. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  4. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  5. DirectShow NEWBIE Tutorial Request
    By WaterNut in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2006, 10:26 PM