Results 1 to 5 of 5
  1. #1
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Question nested for loops

    Hello

    Plz explain nested for loops.

    This is my current understanding:

    the outer for loop starts which has a nested for loop. The inner for loop loops and when done ...............

    please explain if it returns to the outer for loop or the inner for loop.

    thank you.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Simple example of a nested loop:

    Java Code:
    for(int j = 0; j < 3; j ++)
    {
       System.out.println("Outer loop " + j + ".");
       for(int i = 0; i < 3; i ++)
       {
          System.out.println("Inner loop " + i + ".");
       }
    }
    Output:

    Java Code:
    Outer loop 0.
    Inner loop 0.
    Inner loop 1.
    Inner loop 2.
    Outer loop 1.
    Inner loop 0.
    Inner loop 1.
    Inner loop 2.
    Outer loop 2.
    Inner loop 0.
    Inner loop 1.
    Inner loop 2.
    My Hobby Project: LegacyClone

  3. #3
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    thanks for the help, appreciate it. ;)

  4. #4
    steelshark is offline Member
    Join Date
    Aug 2009
    Posts
    25
    Rep Power
    0

    Default

    Try to imagine it like you we're drawing a chessboard:
    you would do this with 2 nested for loops.
    (for int i =0; i < 8; i++)
    (for int j=0; j < 8 ;j++){
    //code to draw
    }
    It will look like this:
    I0J0 I0J1 I0J3 .... I0J7
    I1J0 I1J2 ...
    ...
    I7J0 I1J1 ... I7J7
    Thus for one iteration on the outer for loop, you would go through the entire inner loop.
    I hope this clarifies it a bit.

  5. #5
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by steelshark View Post
    Try to imagine it like you we're drawing a chessboard:
    you would do this with 2 nested for loops.
    (for int i =0; i < 8; i++)
    (for int j=0; j < 8 ;j++){
    //code to draw
    }
    It will look like this:
    I0J0 I0J1 I0J3 .... I0J7
    I1J0 I1J2 ...
    ...
    I7J0 I1J1 ... I7J7
    Thus for one iteration on the outer for loop, you would go through the entire inner loop.
    I hope this clarifies it a bit.
    Perfectly explained, thank you

Similar Threads

  1. Nested Loops
    By ks1615 in forum New To Java
    Replies: 4
    Last Post: 02-18-2009, 02:48 AM
  2. Nested Classes
    By new.guy in forum New To Java
    Replies: 13
    Last Post: 09-07-2008, 04:44 AM
  3. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM
  4. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM
  5. Nested Tags JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-25-2007, 05:42 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •