+ 1

Incrementation

int x = 3; int y = ++x; // x is 4, y is 4 In the above I'm struggling to understand how x is 4 , I get how y is 4 because x = 3 and the increment is infront of the x so its 1+3 =4 Could someone please help

29th May 2019, 4:15 PM
Tracy Mulder
Tracy Mulder - avatar
4 Respostas
+ 1
@Anhjje int x = 3; int y = x++; // x is incremented after it's assigned, so y = 3 x = 4 and y = 3
29th May 2019, 4:33 PM
Cluck'n'Coder
Cluck'n'Coder - avatar
0
x++ and ++x both mean x=x+1 the difference is that x++ returns the value of x first and then increments it and ++x increments x first and then returns its value. doing y=x++ would give y a value of 3 as in this case x is returned first. for y=++x, y is 4 as x is incremented first and then its value is returned. x++ or ++x would both result in incrementation of x.
29th May 2019, 4:27 PM
Farry
Farry - avatar
0
int x = 3; int y = x++; // x is 3, y is 4?
29th May 2019, 4:30 PM
HNNX 🐿
HNNX 🐿 - avatar