Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By jim829

Thread: I want to understand this...

  1. #1
    WHM2 is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default I want to understand this...

    I'm learning Java with Y. Liang's Introduction to Java 9th, comp. It's a solid book so far. I try to make sure I am competent and confident with the current subject matter before moving onto another chapter. I felt I was getting the hang of 'for' loops, when I get this mid-chapter question:

    Show output:
    public static void main(String[] args) {


    for (int i = 1; i<5; i++){
    int j =0;
    while(j<i) {
    System.out.print(j+ " ");
    j++;
    }
    }
    }
    }

    OUTPUT: 0 0 1 0 1 2 0 1 2 3

    On the first iteration, 'j' will print 0, but will increment before the second iteration. Does 'j' revert to 0 (because of int j = 0), or does it increment from 1, 2, 3, 4? I don't understand how the output was generated from the code above. Please help! Thanks!

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: I want to understand this...

    You realize that you have TWO loops right? One (while) inside the other (for). You have to see what happens to 'j' in the outer for loop and in the inner while loop separately.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    WHM2 is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: I want to understand this...

    Thanks for your reply! I know that there are two loops, but I am still new to the intricacies of loops. Does j revert to zero after a certain number of iterations? If so, where in the code is that specified? My Eclipse IDE and the book have the same output so I am out of the loop on this one (no pun intended).

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: I want to understand this...

    You need to "play computer." Get a pencil and paper and make two columns labeled i and j. Each line represents the value of the output. Go thru each loop and write the values as the loops work together. When you are done, look at j column from top to bottom.

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

  5. #5
    WHM2 is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: I want to understand this...

    gimbal2 and jim829, thank you guys for your help! I just figured it out. I was over thinking the problem: I was not incrementing 'j' properly. I appreciate your help!

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,104
    Rep Power
    6

    Default Re: I want to understand this...

    Okay but let me ask you a question anyway: you were aware that in your code example your for loop only loops 4 times and not 5 times?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    WHM2 is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: I want to understand this...

    Yes because i never exceeds 4. That's how I figured it out. I changed to code so that I could understand it better and this is what I got:

    i: 1 j: 0 ( j2: 1) i: 2 j: 0 ( j2: 1) ( j2: 2) i: 3 j: 0 ( j2: 1) ( j2: 2) ( j2: 3) i: 4 j: 0 ( j2: 1) ( j2: 2) ( j2: 3) ( j2: 4)

    j2 is simply j before it increments. I realized I was assuming j would increment in the program and print out its one result after each iteration. Instead, j output its increments until it was == to i each time.

Similar Threads

  1. Can any one help me to understand the Code
    By soomroimran in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 09:23 AM
  2. I cant understand OOP
    By eng_hyzoom in forum New To Java
    Replies: 4
    Last Post: 11-19-2010, 04:10 PM
  3. GUI help. Don t understand
    By s0meb0dy in forum AWT / Swing
    Replies: 2
    Last Post: 10-27-2010, 09:40 PM
  4. Trying to understand
    By ladykrimson in forum New To Java
    Replies: 20
    Last Post: 10-12-2010, 11:10 PM
  5. I don´t understand
    By Manikyr in forum New To Java
    Replies: 6
    Last Post: 02-22-2009, 11:22 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
  •