Results 1 to 5 of 5
  1. #1
    glopez09 is offline Member
    Join Date
    Nov 2009
    Posts
    7
    Rep Power
    0

    Default explanation of this loop?

    hi guys. i'm having trouble understanding the flow of this loop, more specifically, nested loops in general. can somebody walk me through it? its a very small one.

    Java Code:
      for(int row =1; row <= 6; row++){
    			for(int col = 1; col <= row; col++)
    				System.out.print(col + " ");
    			System.out.println();
    it outputs this:

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

  2. #2
    blackpanther is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by glopez09 View Post
    hi guys. i'm having trouble understanding the flow of this loop, more specifically, nested loops in general. can somebody walk me through it? its a very small one.

    Java Code:
      for(int row =1; row <= 6; row++){
    			for(int col = 1; col <= row; col++)
    				System.out.print(col + " ");
    			System.out.println();
    it outputs this:

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4 5 6
    1) in row 1 ->col 1-> col(o/p that is one) at end row ->2 col ->2
    2) in row 2-> col 1&2 -> col(o/p is 2 & 3) col is 2 and 3(col++) at end row->3 col ->3 similar way

  3. #3
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Post

    Quote Originally Posted by glopez09 View Post
    Java Code:
      for(int row =1; row <= 6; row++){
    			for(int col = 1; col <= row; col++)
    				System.out.print(col + " ");
    			System.out.println();
    Allright, you see this whole part:
    Java Code:
    for(int col = 1; col <= row; col++)
    		System.out.print(col + " ");
    	System.out.println();
    ...is repeated by the first for loop (because it is wrapped in "{ }", which starts after the first loop is declared). The updates in the first for loop make sure it repeats 6 times.
    At the second for loop you see no "{ }". That means only this line:
    Java Code:
    System.out.print(col + " ");
    ...will be repeated. The number of times this repeats depends on the status on the first for loop. When the first for loop is on its first cycle, the second for loop will run a cycle only once. In the second cycle (of the first for loop) the second one will give two cycles, ect.

  4. #4
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    you have the outer loop and the inner loop the outer loop never gets reset just increments 1 2 3 4 5 6..but every time the inner loop returns to the outer loop the inner loop gets reset to 1....the second time through the outer loop is at 2 so it prints 1 2. the third time the outer loop is at 3 so its 12 3...i hope this helps

    you are missing an end brace and some braces for your inner loop maybe you just quickly entered the code to give the gist of things
    Last edited by aaroncarpet; 11-14-2009 at 11:43 PM. Reason: noticed pranthetical issue

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    This is the code you've posted here, I've added the closing brace '}' at the end.

    Java Code:
    for(int row =1; row <= 6; row++){
              for(int col = 1; col <= row; col++)
                       System.out.print(col + " ");
              System.out.println();
    }
    Then I've done a small change as follows to the inner nested for loop. I've wrap the code block with braces pair {}. Why I do that? It's easy to read, and also easy to understand for others, even for you in later time.

    Java Code:
    for(int row =1; row <= 6; row++){
              for(int col = 1; col <= row; col++) {
                       System.out.print(col + " ");
              }
              System.out.println();
    }
    As a practice try to do this, even your code block contains only one line of code.

    You've lots of explanation about the code, so I'm not going to comment on that. Still you have something confusing in the code, let us know. Good luck!

Similar Threads

  1. File Reading Explanation
    By GraemeH in forum New To Java
    Replies: 2
    Last Post: 04-03-2009, 06:45 PM
  2. Quick Sort explanation.
    By hawaiifiver in forum New To Java
    Replies: 4
    Last Post: 03-10-2009, 03:28 AM
  3. Class explanation
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 07:50 PM
  4. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-14-2007, 12:39 AM
  5. I need didactic explanation
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 06:37 AM

Posting Permissions

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