1

Possible Duplicate:
What is x after “x = x++”?

In a loop I have:

int x = 0;
while(int x < 10){
x = x++;
}

Why does this not work?

Community
  • 1
  • 1

4 Answers4

3

Change x = x++ to just x++. x++ is unary operation and you don't need to use the assignment operation.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102
3

Post increment operator uses the value first and then increments.

x=x++;

Here, we assign X to X (zero to zero) and then we increment X to 1 but we never assign it to anything.

you can change to

X=++X; 

and this should give you what you want.

Mechkov
  • 4,294
  • 1
  • 17
  • 25
1

Well... try this instead.

int x = 0;
while(int x < 10){
x++;
}
Gabriel
  • 3,039
  • 6
  • 34
  • 44
1

Try converting

x = x++;

to

y = x++; // i.e. y = x, then increment x
x = y; 

to understand it.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127