# Page one!

• August 22nd, 2011, 07:48 PM
hal_v_1937
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.
• August 22nd, 2011, 09:31 PM
Junky
Re: Page one!
Quote:

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.