+ 9
I know this is bad practice but why is the answer 5 and not 6?
23 ответов
+ 6
Sonic
let as have a auxiliary variable
int k=5;
int x=k++;
k=x;
now if you put the value of x instead of x you will get your expression which is
k=k++;
and the value of k will not be changed
+ 5
Alpha5and5 #2yrsold that case is different. In my case, a post incremented variable is assigned back to itself instead of another variable. I know how standard post increment works but not when the variable is reassigned to itself using the = operator.
+ 5
Yes so the "k =" part assigns 5 to k and at this point k will be 5. But immediately after that statement, why is k not incremented to 6 using the k++ ?
+ 4
We have this process here:
1. Left hand side is evaluated (this is a variable k)
2. Right hand side is evaluated (old value of k) and this value is what to be assigned to left hand side.
3. Post increment occurs.
4. Old value of k (evaluated in step 2) is assigned to left hand side.
So ee have no incrementation.
+ 4
K.M Ahnaf Zamil , Coder Kitten has explained it well.
+ 3
JavaGoal.com yes I know the difference between pre and post increment but why does post increment not work if k is assigned back to itself?
+ 3
ABADA S thanks but why is it different when k is assigned back to itself?
+ 3
it will not be different because the right expression will be evaluated then assigns it to the left one
in other words
all expression in the left will be done so after finishing that expression the value of (k++) will returns k as you know and k will be 6
when doing assignment it will assign the result (which is 5) to k
that means the old value of k (which is 6) will be replaced by the new one (which is 5)
+ 3
Sonic as I said evaluation of the value which will be assigned to left hand side is done in step 2 and this value will be held.
Incrementing k doesn't affect this value (step2) and is an independent operation.
+ 3
If you want to increment k then use k++;. The only reason to use the (variable)=k++ is to be able to store the original value of k in a new variable and then increment the original value in k.
+ 3
Coder Kitten thanks a lot!
+ 3
Joey Boggess don't spam.
+ 3
Check this bro
https://code.sololearn.com/cT33Hq4Vf6CB/?ref=app
+ 2
Qasem I don't understand why the step 4 you mentioned takes place.
+ 2
Sonic I have checked your code. I probably solved it. If you put ++k instead of k++, then it outputs 6. It's because the incrementation is post, that means it will increment the variable after it is used. So you printed out the variable before it was incremented. But if you use pre incrementation (++k), it will increment the variable before it is used. Post incrementation is used in while loops for iterating (in most cases). So you should use pre incrementation or use k = k+1 or k+ = 1
https://code.sololearn.com/cmUCVXrcDODh/?ref=app
+ 2
Because k++ firsts use the value then increases it
+ 2
Sonic Well I wish I could've helped you
+ 1
try this
public class Program
{
public static void main(String[] args) {
int k=5;
k++;
k=k++;
System.out.println(k);
}
}
or
public class Program
{
public static void main(String[] args) {
int k=5;
k++;
System.out.println(k);
}
}
for 6
+ 1
This is because
k++ means : first the value of k will be "USED" and then it will get incremented.
Initially the value of k is 5.
So the value of k is used
(k=k++) and then it gets updated.
But the twist here is that the value gets stored in k itself bcoz the value is first "USED"
So the answer is 5 and not 6
I hope you get it.
Cheers.