Aleksey Movchan

Ranch Hand

Posts: 49

1

Aleksey Movchan

Ranch Hand

Posts: 49

1

posted 2 months ago

**Knute Snortum**it works for any a. There obviously is a declaration before since it's Java

posted 2 months ago

- 1

As long as a has a value is not near MAXVALUE or MINVALUE the result is always the same for this and a equals 2, for int related primitives that is.

I kind of know the answer, but I can not articulate it too well. I'll give it a try:

Whenever you do a post fix operation like a-- or a++ then you first evaluate a for the equation then do the post fix operation. Whenever you do a prefix operation like --a or ++a then you first do the prefix operation then evaluate a for the equation. When you have both post fix and prefix occurring in the same equation then you evaluate the values left to right.

I kind of know the answer, but I can not articulate it too well. I'll give it a try:

Two choices: A) Learn from mistakes. B) Don't make mistakes.

Aleksey Movchan

Ranch Hand

Posts: 49

1

Campbell Ritchie

Marshal

Posts: 56520

172

posted 2 months ago

If you go through the this FAQ, I think it will give you an explanation. Knute is right; you must have an initial value for your

`int`otherwise it might not compile. But I see what you mean; it will print 2 from any starting value. Even if there is an arithmetic overflow:-java Minus2Demo 1 2 69 4 2147483647 -2147483648 2147483646 -2147483647

2

2

2

2

2

2

2

2

Piet Souris

Master Rancher

Posts: 2041

75

Campbell Ritchie

Marshal

Posts: 56520

172

posted 2 months ago

In the case of preincrement, the value of ++

You are 100% right about left to right.

I was taught that underflow only occurs with floating‑point arithmetic; what we are experiencing is overflow in one direction or the other.

- 1

Please try values close to those extreme values; when I tried it earlier I got 2 even for extreme values with overflow. I think if you decrement a value twice and then subtract you will get 2 irrespective of the starting value. Any overflow will cancel itself out.Pete Letkeman wrote:As long as a has a value is not near MAXVALUE or MINVALUE . . .

I think that is a bit too much of an oversimplification. I think it is more accurate to say that there are two values: the value of. . . first evaluate a for the equation then do the post fix operation. . . .

__and the value of the__

*i*alone__whole expression__;In the case of postincrement, the value of

*i*++*i*++ is equal to the

__old value of__, and that causes no end of confusion to beginners. The value of

*i**i*is hidden where you cannot find it until the next expression.

In the case of preincrement, the value of ++

*i*is equal to the

__new value of__, and that seems to be really easy to understand.

*i*You are 100% right about left to right.

I was taught that underflow only occurs with floating‑point arithmetic; what we are experiencing is overflow in one direction or the other.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |