newbie request..(Please)

This is a discussion on newbie request..(Please) within the C++ Programming forums, part of the General Programming Boards category; Hi all I do understand nested for-loop when two for-loop is used but i get puzzled in case of three ...

  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
    21,456
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,409
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21