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 6 of 6

Thread: Why does this work?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why does this work?

    I understand how loops and nested loops work. But in the code below, I don't see why one * is printed on the first line, two on the second, etc., instead of just a column of *'s. I'm probably missing something missing very simple. Can someone explain this to me?
    -------------

    What is the output of the following nested for loops?
     
    	for (int i = 1; i <= 5; i++) {
    	    for (int j = 1; j <= i; j++) {
    	        System.out.print("*");
    	    }
    	    System.out.println();
    	}
    Output:
    	*
    	**
    	***
    	****
    	*****
    Last edited by pbrockway2; December 15th, 2012 at 06:37 PM. Reason: code tags added


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    975
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Why does this work?

    Are you happy with the difference between print() and println()?

    By my count that code should print 20 characters: 15 asterisks and 5 newlines. Walk through the code and make sure you can account for all 20 characters.

    ---

    Don't forget to use "code" tags with your posts. [code]<your code here>[/code].

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does this work?

    Oh yes, I understand the difference between print() and println(). But I don't see how there should be 20 asterisks. For example, initially the i=1, so j=1, one asterisk gets printed, and the println statement causes the advancement to the next line. But I'm not sure why the next line has two asterisks and how j++ and i++ play into this.

    Sorry about the code tags. I've never added code to this site before.

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    975
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Why does this work?

    Think about that outer loop.

    First i is 1. You are right: j is made to go from 1 to 1 (ie from 1 to i). So one asterisk is printed. And then the newline. That completes one pass through the outer loop.

    The second verse is the same as the first but with i equal to 2. So j is made to go from 1 to 2 (ie from 1 to i). So two asterisks are printed. And then the newline.

    The next time around the outer loop i is 3 and the inner loop will go from 1 to 3, printing an asterisk each time to give 3.

    etc.

    Key to the behaviour of the code is that in the inner loop j always starts at 1, but is made to increase to a bigger and bigger value each time because i will be bigger each time the inner loop executes.

    ---

    The i++/j++ expressions are there to say how i and j get incremented at the end of the loop. In both cases they are saying that the loops (both of them) increment the loop variable by one.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why does this work?

    Of course! Good explanation. I see now that what I was doing was that after i increased to 2, I was reading j = i, when I should have read j<=i, which in essence resets j back to 1 and allows it to increase to 2. Thanks!

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    975
    Thanks
    7
    Thanked 205 Times in 181 Posts

    Default Re: Why does this work?

    You're welcome, I'm glad you've got it sorted out.

Similar Threads

  1. how does this work?
    By somebodyonearth in forum Java Theory & Questions
    Replies: 3
    Last Post: March 10th, 2012, 01:23 PM
  2. Does this work?
    By marmanq in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 20th, 2012, 10:51 AM
  3. I cant work it out!!!
    By tuts73 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: January 18th, 2012, 10:16 AM
  4. Do you work out?
    By thesolo in forum Totally Off Topic
    Replies: 4
    Last Post: October 24th, 2011, 09:03 AM
  5. Why does this not work?
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 10:47 AM