Tse Wu

Greenhorn

Posts: 10

matt love

Ranch Hand

Posts: 67

posted 5 years ago

- 1

Hi Tse,

to resolve what’s on the right of the equals:

1: obtain the value of y first which is 1 before decrimenting (since this is a post-decriment),

2: then decriment y by –1 to 0,

3: again since you are post-decrimenting, obtain the value of y first, which is now 0,

4: decriment y to –1;

so, y = 1 (from step 1 above) + 0 (from step 3 above) = 1

notice the last decriment had no effect on the equation.

Matt

to resolve what’s on the right of the equals:

1: obtain the value of y first which is 1 before decrimenting (since this is a post-decriment),

2: then decriment y by –1 to 0,

3: again since you are post-decrimenting, obtain the value of y first, which is now 0,

4: decriment y to –1;

so, y = 1 (from step 1 above) + 0 (from step 3 above) = 1

notice the last decriment had no effect on the equation.

Matt

Tse Wu

Greenhorn

Posts: 10

matt love

Ranch Hand

Posts: 67

posted 5 years ago

Hi Tse.

I didn't quite follow your response.

If you are willing, let's take it step-by-step, with separate emails for each step, and see if there is a particular step that you're having difficulty with.

int y = 1;

y = y-- + y--;

we will resolve the expression left-to-right, so we will first address the first y--.

i believe y-- is called a post-decriment operator because we will evaluate y (or another phrase might be to obtain the value of y) first and then decriment y by 1.

--y would be called a pre-decriment operator because we would decriment y before we would evaluate and obtain the value of y.

so, back to our expression: we have evaluated and retained the value of y as 1.

we are still working with the leftmost y-- and we haven't decrimented it yet.

ok so far?

Matt

I didn't quite follow your response.

If you are willing, let's take it step-by-step, with separate emails for each step, and see if there is a particular step that you're having difficulty with.

int y = 1;

y = y-- + y--;

we will resolve the expression left-to-right, so we will first address the first y--.

i believe y-- is called a post-decriment operator because we will evaluate y (or another phrase might be to obtain the value of y) first and then decriment y by 1.

--y would be called a pre-decriment operator because we would decriment y before we would evaluate and obtain the value of y.

so, back to our expression: we have evaluated and retained the value of y as 1.

we are still working with the leftmost y-- and we haven't decrimented it yet.

ok so far?

Matt

Tse Wu

Greenhorn

Posts: 10

posted 5 years ago

Hi Matt,

From my understanding.

Step1: Since the first y is post decrement, the first y value is 1.

Step2: After the resolution of the first y, y is decreased to 0.

Step3: The second y is also post decrement, so the second y value is assigned to 0.

Step4: after the resolution of the second y, y is decreased to -1.

Step5: so we ended up with 1 + 0

Step6: At this point y is -1, but since 1 + 0 is 1 the assignment of y changed from -1 to 1 at this point.

I think this is how it goes with the explanation you provided which made sense. Let me know if one of the steps was wrong. Thanks again.

From my understanding.

Step1: Since the first y is post decrement, the first y value is 1.

Step2: After the resolution of the first y, y is decreased to 0.

Step3: The second y is also post decrement, so the second y value is assigned to 0.

Step4: after the resolution of the second y, y is decreased to -1.

Step5: so we ended up with 1 + 0

Step6: At this point y is -1, but since 1 + 0 is 1 the assignment of y changed from -1 to 1 at this point.

I think this is how it goes with the explanation you provided which made sense. Let me know if one of the steps was wrong. Thanks again.

OCPJP 1.6

matt love

Ranch Hand

Posts: 67

posted 5 years ago

You have it correct to the end where you're a little off.

int y = 1;

y = y-- + y--;

Step1: Since the first y is post decrement, the first y value is 1.

yes, y is evaluated as having value 1.

Step2: After the resolution of the first y, y is decreased to 0.

Yes

Step3: The second y is also post decrement, so the second y value is assigned to 0.

correct

Step4: after the resolution of the second y, y is decreased to -1.

yep

Step5: so we ended up with 1 + 0

yep

Step6: At this point y is -1, but since 1 + 0 is 1 the assignment of y changed from -1 to 1 at this point.

I think you have the concept down Tse, although y did not change from -1 to 1 at the end. It’s more a matter of snapshots in time. As far as the expression is concerned, y resolves to 1 at the end because of the progression you described.

After the expression, y is in fact –1, because of that final post-decrementer. That second y-- evaluated to 0 so the expression could be resolved (it is a post-decrementer) and then that second y-- decremented y by 1.

int y = 1;

y = y-- + y--;

System.out.println(y); // y is –1 at this point after the above entire expression is executed, including that final post-decrement;

That final decrement, --, has the final effect on the value of y after the expression was completely and finally completed. Remember, the value of y in the second y—was evaluated before it was decremented.

I know it looks weird. Be sure and let me know if you’re still hung up on something and I’ll respond tomorrow.

Matt

int y = 1;

y = y-- + y--;

Step1: Since the first y is post decrement, the first y value is 1.

yes, y is evaluated as having value 1.

Step2: After the resolution of the first y, y is decreased to 0.

Yes

Step3: The second y is also post decrement, so the second y value is assigned to 0.

correct

Step4: after the resolution of the second y, y is decreased to -1.

yep

Step5: so we ended up with 1 + 0

yep

Step6: At this point y is -1, but since 1 + 0 is 1 the assignment of y changed from -1 to 1 at this point.

I think you have the concept down Tse, although y did not change from -1 to 1 at the end. It’s more a matter of snapshots in time. As far as the expression is concerned, y resolves to 1 at the end because of the progression you described.

After the expression, y is in fact –1, because of that final post-decrementer. That second y-- evaluated to 0 so the expression could be resolved (it is a post-decrementer) and then that second y-- decremented y by 1.

int y = 1;

y = y-- + y--;

System.out.println(y); // y is –1 at this point after the above entire expression is executed, including that final post-decrement;

That final decrement, --, has the final effect on the value of y after the expression was completely and finally completed. Remember, the value of y in the second y—was evaluated before it was decremented.

I know it looks weird. Be sure and let me know if you’re still hung up on something and I’ll respond tomorrow.

Matt