Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By jim829
  • 1 Post By sibernewf
  • 1 Post By jim829

Thread: Cannot figure out basic nested for loop

  1. #1
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Cannot figure out basic nested for loop

    Java Code:
    for (int count = 0, count <=3; count++)
    for (int count2 = 0; count2 < count; count2++)
    System.out.println(count2);
    Iteration 1: count = 0, count2 = 0 , 0<0 = False->>> No output
    Iteration 2: count = 1, count2 = 0, 0 < 1 = True--->>> 0
    Iteration 3: count = 2, count2 = 0, 1 <2 = True -->>> 0
    Iteration 4: count = 3, count2 = 1, 1 < 3 = True ->>>> 1
    Iteration 5: count = Completed?, count2 = ?

    I know that a 0 comes next, at some iteration, but from here I start getting confused.
    Last edited by no_slip_condition; 06-06-2013 at 03:07 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Cannot figure out basic nested for loop

    Sometimes it's best to just use a pencil and paper. Think about what the inner loop does for each value of count.
    count = 0, inner loop immediately exits
    count = 1, count2 = 0
    count = 2, count2 = 0,
    count = 2, count2 = 1
    count = 3, count2 = 0
    count = 3, count2 = 1
    count = 3, count2 = 2

    Regards,
    Jim
    no_slip_condition likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Cannot figure out basic nested for loop

    Believe me, I've tried many times, but my fundamental understanding is flawed. Why does the outer loop pause on 2? That makes no sense to me. If you run the outer loop by itself, it just goes 0, 1, 2, 3. I also don't get why count2 reverts to 0, does that occur because the outer loop stops at 3?

  4. #4
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default Re: Cannot figure out basic nested for loop

    The outer loop simply pauses while its iterating through the inner loop. Once the inner loop is finished, the outer loop increments by 1 then pauses while the inner loop goes again. and so on...

    Hope that helps.
    no_slip_condition likes this.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Cannot figure out basic nested for loop

    A loop simply tells whatever is within the scope of the loop a certain number of times. If that happens to be another loop, then it repeats the inner loop a certain number of times. Try the following:

    Java Code:
    for (int outer = 0; outer < 5; outer++) {
       System.out.println("About to start inner loop, outer = " + outer);
       for (int inner = 0; inner < 5; inner++) {
           System.out.println("Inner loop running, inner = " + inner);
       }
       System.out.println("Inner loop done, returning to next iteration of outer loop");
    }
    Another way to view your original nested loops is the following:


    Java Code:
    for (int count2 = 0; count2 < 0; count2++)
    System.out.println(count2);
    
    for (int count2 = 0; count2 < 1; count2++)
    System.out.println(count2);
    
    for (int count2 = 0; count2 < 2; count2++)
    System.out.println(count2);
    
    for (int count2 = 0; count2 < 3; count2++)
    System.out.println(count2);
    Regards,
    Jim
    Last edited by jim829; 06-06-2013 at 05:20 AM.
    no_slip_condition likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Join Date
    May 2013
    Posts
    16
    Rep Power
    0

    Default Re: Cannot figure out basic nested for loop

    Ahh, I get it now. Thank you very much guys. The example you posted helped a lot too.

Similar Threads

  1. Replies: 0
    Last Post: 11-01-2012, 12:58 AM
  2. need help to figure out basic java program
    By shane123 in forum New To Java
    Replies: 21
    Last Post: 12-02-2011, 04:12 AM
  3. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 05:10 PM
  4. Basic Java applet error, cant figure it out!
    By alacn in forum Java Applets
    Replies: 3
    Last Post: 08-03-2010, 06:25 PM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM

Posting Permissions

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