Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Nested For Loops

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Nested For Loops

    I can't understand how these 'nested' loops work. Can anyone try explain to me in the most simplest form why they are needed and how they work?
      public static void main(String[] args)
       {
     
          for( int count = 1; count <= 5; count ++)
          {
     
             for( int count2 = 1; count2 <= count; count2 ++)
             {
                System.out.print(count2 + "\t");
             }
     
             System.out.println();
     
          }
       }//main
    }//class
    Last edited by javapol; February 21st, 2013 at 06:01 AM. Reason: [code=java] moved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Nested For Loops

    What is printed out when the code is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Nested For Loops

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Nested For Loops

    How is the value of the variable: star changed to the digits that you posted as the program's output?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Nested For Loops

    Sorry wrong code, edited now.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Nested For Loops

    Looks the same to me.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Nested For Loops

    Thats it now,

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Nested For Loops

    Have you tried playing computer with the code? Use a piece of paper to write down the values of the variables as each statement is executed.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    61
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Nested For Loops

    I just need to know what happens in nested loops, not that particular program, I mean if a loop loops something 10 times and a loop outside it loops 5 times does this mean that the inside is looped 50 times?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,562
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Nested For Loops

    inside is looped 50 times
    5*10 = 50
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: Nested For Loops

    The compiler reads from top-down, so it will enter the outer for-loop and executes the code inside of it (i.e. inner for-loop). Once the inner for-loop has finished iterating, the outer for-loop iterates once more and executes the inner for-loop again. This continues until the outer for-loop has finished iterating.

    Here's a very simple example I made on the spot:

     
    import java.util.Scanner;
     
    public class something {
     
         public static void main(String[] args) {
     
               Scanner input = new Scanner(System.in);
     
               while(true) {
     
                    System.out.println("Enter 'play' to play or 'no' to not play");
                    String data = input.nextLine();
     
                    if(data.equalsIgnoreCase("no")) {
                         break;
                    }
     
                    while(data.equalsIgnoreCase("play")) {
                         // more code
                    }
               }
          }
    }

    If the user enters "no", then the outer loop ends and the code within the inner loop is never executed. On the other hand, if the user enters, "play", then the code within the inner loop is executed. If the user enters anything else, then the inner loop is not executed and since there is no else statement with the if-statement, the outer loop re-iterates until the user enters valid input.

    Not all programs will use nested loops, you only have nested loops in your program if it requires them. There generally are many ways of writing code to achieve the same functionality.

Similar Threads

  1. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 10:43 PM
  2. Something wrong with my nested loops? Please help.
    By MyNamesMatt in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 23rd, 2012, 06:27 PM
  3. Nested for loops and array
    By bryanboy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2011, 05:45 AM
  4. Using Nested Loops
    By m2msucks in forum Loops & Control Statements
    Replies: 7
    Last Post: November 5th, 2011, 07:05 PM
  5. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM