i see the postfix x=5; y=x++; //x is 6,y is 5 so my question is that why x is 6 not 5 and y is 5 not 6?? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 2

i see the postfix x=5; y=x++; //x is 6,y is 5 so my question is that why x is 6 not 5 and y is 5 not 6??

10th Sep 2016, 4:46 PM
aniket singh
4 Respuestas
+ 7
By writting x++, your basically saying to the console to add 1 to the value of x AFTER it gives it to y, thats why(lol the pun) it says that x has a value of 6 and y a value of 5. When you put x++ or ++x, on the first form, it adds the value (1) AFTER it does the operation (in this case, giving the same value to y making it x=6 and y =5 ) while on the second form, the value is added BEFORE the operation is performed(resulting in x=6 and y =6). Hope it helps. Return 0;
10th Sep 2016, 5:07 PM
Jklow
Jklow - avatar
+ 2
++ is applied to x, not y.
10th Sep 2016, 5:01 PM
Zen
Zen - avatar
+ 1
Y=X++ is postfix operation which means 1) value of x is returned first and assigned to y. Hence y=5 2) then x is incremented. Hence x becomes 6
10th Sep 2016, 6:02 PM
Sujit Sasi
Sujit Sasi - avatar
- 2
djdj
10th Sep 2016, 6:10 PM
Dmitry