• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't understand logic  RSS feed

 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone explain please why after

a equals 2?

What is the first step in calculating, second etc..
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a declaration for a?
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum it works for any a. There obviously is a declaration before since it's Java
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming int a = 0;

1) use a, then decrement, so 0, then a == -1
2) decrement a, then use it, so -2
3) 0 - -2 = 2, so a = 2
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksey Movchan wrote:Knute Snortum it works for any a.

Really?  Work it out for a = 5;
 
Aleksey Movchan
Ranch Hand
Posts: 49
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thank you!
 
Pete Letkeman
Ranch Foreman
Posts: 898
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  •  
    Aleksey Movchan
    Ranch Hand
    Posts: 49
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:
    Really?  Work it out for a = 5;




    Try it ;)
     
    Knute Snortum
    Sheriff
    Posts: 4270
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah yes, you are correct!
     
    Campbell Ritchie
    Marshal
    Posts: 56520
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, the outcome isn't very surprising. The right had side boils down to: a - (-1 + (a-1)).
    And since MIN - 1 = MAX, underflow makes no difference.

    Without using a pc: what is the outcome of: a = --a - a--? And does underflow make a difference?
     
    Campbell Ritchie
    Marshal
    Posts: 56520
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Pete Letkeman wrote:As long as a has a value is not near MAXVALUE or MINVALUE . . .
    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.
    . . . first evaluate a for the equation then do the post fix operation. . . .
    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 i alone and the value of the whole expression i++;In the case of postincrement, the value of i++ is equal to the old value of i, and that causes no end of confusion to beginners. The value of 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 i, and that seems to be really easy to understand.
    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.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!