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

Thread: Page one!

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Page one!

    G'day,

    Congratulations on the set up of the site, very easy, very professional and most welcoming.

    I am a total novice to Java and am just working through the on line tutorials, my programming experience was with a TRS COCO II using BASIC many, many years ago.

    I already have a query, if someone could "lend a Hand"

    hal

    here's my query, an extract from the tutorial page:-

    Consider the following code snippet:

    int i = 10;
    int n = i++%5;

    Question: What are the values of i and n after the code is executed?
    Answer: i is 11, and n is 0

    My problem is :- Does the second line (int n=1++%5 alter the original value of "i" in line one?
    I would have thought all it did was to set the value of n as i+1 leaving i = 10, then do the modulus part.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Page one!

    Does the second line alter the original value of "i"
    Obviously it does considering the answer says the value of i is 11 not 10.

    You have to break the line down into steps. The right hand side of statement is evaluated first and it is evaluated left to right.
    int n = i++ % 5;
    1. Retrieve the value stored in variable i
    int n = 10 % 5; (i++ still has to happen and since it is postincrement it happens last)
    2. Perform mod calculation. 10 divided by 5 equals 2 with 0 left over.
    int n = 0; (i++ still has to happen)
    3. Increment i
    int n = 0;
    4. Assign result to n

    So we now can see that i has been incremented from 10 to 11 and 0 is assigned to n.
    Improving the world one idiot at a time!

Similar Threads

  1. Open web page according to day
    By caenleve in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 8th, 2011, 07:22 PM
  2. showing page
    By ighor10 in forum Java Theory & Questions
    Replies: 1
    Last Post: August 26th, 2010, 08:52 AM
  3. reg how to use log in jsp page
    By javaking in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: April 9th, 2010, 12:36 AM
  4. Need help in JSP Page
    By vinothkumarrvk in forum Web Frameworks
    Replies: 1
    Last Post: March 12th, 2010, 07:43 AM
  5. Need help in JSP Page
    By vinothkumarrvk in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: March 9th, 2010, 05:20 AM