0
Difference between prefix and postfix
I still does not understand about prefix and postfix statements.. what is the difference ??
3 ответов
+ 4
Take this example:
x = 2;
y = ++x;
// y is 3, x is 3
Whereas something like this is:
x = 2;
y = x++;
// y is 2, x is 3. It does y = x and then
// increments it.
0
x=2
y=++x
means you add first 1 to x then you assign the value x to y. thats why the answer is y=3
x=2
y=x++
means you assign the value of x to y first then you add 1 to x. so the answer will be y=2
0
++x is prefix increment. it will add 1 to the variable 'x' in place.
x+=1
is the same as:
++x
x++ is postfix increment. it will first return the variable and only then add 1 to it.
temp=x
x+=1
return temp
is the same as:
x++