Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Toll

Thread: nested for loop confused with result

  1. #1
    sayswho is offline Member
    Join Date
    Jul 2013
    Location
    Dublin, CA
    Posts
    12
    Rep Power
    0

    Default nested for loop confused with result

    Hi,

    I have this code:
    Java Code:
    for (int i=0;i<=5;i++)//this are rows  -6 - outer loop will be executed 6 times
    		{
    			for (int j=0;j<=i;j++) //this are char numbers- inner loop will execute 6 times
    			{
    				System.out.print("*");
    			}
    			System.out.print("\n");
    		}
    The output is:

    Java Code:
    *
    **
    ***
    ****
    *****
    ******
    So here is what I don't understand:
    For line 1 outer loop i=0; then goes to inner loop j is initialised to 0 so 0<=0 is true;
    then it prints *. And then j is incremented?

    Or it prints * then does
    Java Code:
    System.out.print("\n");
    then it goes back to outer loop i is incremented and now i=1; and then j is incremented?
    And line 2:
    **

    i=1;j=1; so 1<=1 true and then it should print *..increment j =2 test the condition 2 is not <=1 and it should exit inner loop? how do I get ** printed?

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: nested for loop confused with result

    Let's go through exactly what happens.

    When i is 0, then the inner loop will happen while i is less than or equal to zero, i.e. one step.
    When i is 1, then the inner loop will happen while i is less than or equal to one, i.e. two steps. Remember that j is set to 0 at the start of _every_ inner loop, just not the first one.
    When i is 2, the inner loop will happen three times (for j==0, j==1 and j==2).
    And so on.
    sayswho likes this.

  3. #3
    sayswho is offline Member
    Join Date
    Jul 2013
    Location
    Dublin, CA
    Posts
    12
    Rep Power
    0

    Default Re: nested for loop confused with result

    Thank you yes..it just hit me yesterday that because the 2nd loop is inside of the outer loop that means that variables inside of it get destroyed every time the iteration of outer loop is over; re-read info about variable scope

Similar Threads

  1. Nested loop
    By Shasool in forum New To Java
    Replies: 2
    Last Post: 10-23-2011, 06:10 PM
  2. Help in printing the result of the for loop
    By maas in forum Java Servlet
    Replies: 1
    Last Post: 05-12-2011, 10:30 AM
  3. Nested Loop
    By sehudson in forum New To Java
    Replies: 2
    Last Post: 03-11-2011, 04:39 AM
  4. can some one help me with nested loop?
    By keycoffee in forum New To Java
    Replies: 10
    Last Post: 01-25-2010, 03:49 AM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02: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
  •