+ 2

when x++ exactly increment the value of var?

Hi all. I've problem with x++ and its that when exactly it increase the value of var. ie: x=1; y=2+x+4*(x++)+3*(x++*5); what will happen here? is it correct to say that the second sentence mean y=2+x+4*x+3*x*5;x+=2; or y=2+x+4*x+3//now x_new=x+1; then continue *(x_new*5);

10th Nov 2016, 4:24 PM
Amin Ghasemi
Amin Ghasemi - avatar
2 Réponses
+ 6
each time the command x++ is evaluated, the program takes the current value of x for its calculation and after that increment the x value in the confusing code above x++ occurs twice y=2+x+4*( x++ )+3*( x++ *5); ^ ^ evaluated evaluated as 1 and as 2 and then incremented incremented to 3 to 2 so the final value of y could be portrayed as 2+1+4*(1)+3*(2*5) = 37 and the final value of x is 3
10th Nov 2016, 4:37 PM
Burey
Burey - avatar
+ 1
dude no need to panic when it is a=x++ it will first give the value of x to a and then add 1 and if its a=++x then it will first add 1 and then give value of x to a.It is explained in simple words if you understood then like :-)
10th Nov 2016, 4:39 PM
Meet Turakhia
Meet Turakhia - avatar