+ 9

I know this is bad practice but why is the answer 5 and not 6?

https://code.sololearn.com/c12dcCyctNZE/?ref=app

17th Dec 2019, 4:53 AM
Sonic
Sonic - avatar
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
17th Dec 2019, 5:04 AM
ABADA S
ABADA S - avatar
+ 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.
17th Dec 2019, 10:45 PM
Sonic
Sonic - avatar
+ 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++ ?
17th Dec 2019, 10:52 PM
Sonic
Sonic - avatar
+ 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.
17th Dec 2019, 5:11 AM
Qasem
+ 4
K.M Ahnaf Zamil , Coder Kitten has explained it well.
18th Dec 2019, 11:53 AM
Sonic
Sonic - avatar
+ 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?
17th Dec 2019, 5:12 AM
Sonic
Sonic - avatar
+ 3
ABADA S thanks but why is it different when k is assigned back to itself?
17th Dec 2019, 5:14 AM
Sonic
Sonic - avatar
+ 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)
17th Dec 2019, 5:20 AM
ABADA S
ABADA S - avatar
+ 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.
17th Dec 2019, 5:22 AM
Qasem
+ 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.
17th Dec 2019, 6:38 AM
Bernie Booth Jr
Bernie Booth Jr - avatar
+ 3
Coder Kitten thanks a lot!
18th Dec 2019, 5:29 AM
Sonic
Sonic - avatar
+ 3
Joey Boggess don't spam.
18th Dec 2019, 10:12 PM
Sonic
Sonic - avatar
19th Dec 2019, 1:22 AM
Tarun Kumar
Tarun Kumar - avatar
+ 2
Qasem I don't understand why the step 4 you mentioned takes place.
17th Dec 2019, 5:16 AM
Sonic
Sonic - avatar
+ 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
18th Dec 2019, 6:45 AM
Ahnaf
Ahnaf - avatar
+ 2
Because k++ firsts use the value then increases it
18th Dec 2019, 4:13 PM
Naman Pandey
Naman Pandey - avatar
+ 2
Sonic Well I wish I could've helped you
18th Dec 2019, 6:07 PM
Ahnaf
Ahnaf - avatar
+ 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
18th Dec 2019, 4:28 AM
Ravi Tiwari
+ 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.
18th Dec 2019, 4:03 PM
Anmol Pal
Anmol Pal - avatar