Things you need to know:
k(operator)=value
is basically k=k(operator)value
(more info here).
++
is incrementation --
is decrementation. There exists pre, and post versions of this operators where:
x++
(post-incrementation) will first return current value of x
then will increment x
(if x=2
print(x++)
would first pass 2
to method then increment x
to become 3
)
++x
(pre-incrementation) will first increment value of x
then will return it
++
--
have higher precedence than *
/
and these have higher precedence than =
- dividing integers will also return integer so
7/3
will become 2
not 2.33333...
- in
argument1 (operator) argument2 (operator) argument3
argument1
will be evaluated before argument2
, and argument2
will be evaluated before argument3
(in other words they are evaluated from left to right)
variable = expression
before putting value of expression
into variable
it will need to be evaluated first, so x=x+1
first x+1
expression will need be calculated, then x
can be set with result of this evaluation.
so you can rewrite your code as
k/=--k;
-> k = k / --k;
-> k = (k / (--k))
k*=k++;
-> k = k * k++;
-> k = (k * (k++))
k*=++k;
-> k = k * ++k;
-> k = (k * (++k))
So at start with k=12
.
k = (k / (--k))
k = (12 / (--k))
lets replace first k
with its current value
k = (12 / 11)
pre-decrement --k
will first decrement k
to 11
, then return decremented value
k = 1
because we are dividing integers 12/11
result is 1
not 1.0909...
So now k=1
. Time for k*=k++;
k = (k * (k++))
k = (1 * (k++))
k = (1 * (1))
post-increment k++
first returns current value of k
which is 1
then increments it
k = 1
1*1=1
, nothing special here
So k
is still 1
. Time for k*=++k;
k = (k * (++k))
k = (1 * (++k))
since k=1
k = (1 * (2))
++k
first increments then returns value, so k
becomes 2
and this value is returned
k = 2
1*2 = 2
nothing special here.