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

1. ## 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. ## 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.