java - how does pre - increments and post increments work? -


suppose have initialised 2 variables this

int a=0; int b=0; 

now if assign b value

b=a++ + ++a + ++a; 

now a=3 , b=5 shouldnt have been b=2 ? why b assigned value 5 ?

lets see:

  • a++ = 0, afterwards increased 1.

  • ++a = (the 1 a++ plus 1 preincreased) 2

  • ++a = (the 2 ++a above plus 1 preincreased) 3

in total: 0 + 2 + 3 = 5

this explains why three. in last step increased three.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -