-1

What is the difference between if(++x < 0){something} and if(x + 1 < 0){something}

Thanks in advance

Sunny267
  • 27
  • 6

2 Answers2

1

++x increases x by one and returns the result. x + 1 leaves x as is and returns its value increased by one. So the difference is in the value of x after the operation.

The context (inside if condition or not) is irrelevant here.

aparpara
  • 2,171
  • 8
  • 23
  • So does this mean that for example, x = -1, if (++x < 0) means it is actually if (0 < 0) which will return false and x become 0. Where if (x + 1 < 0) will also return false, but x stays the same, 0? – Sunny267 Feb 15 '21 at 15:25
  • In case of `x + 1` `x` will remain `-1`. Actually, you can test it yourself with Coliru http://coliru.stacked-crooked.com/a/89891cb0c0cd6054 – aparpara Feb 15 '21 at 15:47
1

The ++x increments the value of x immediately by 1 and this new value of x is compared with 0.

While x+1 does not increment the value of x and it's original value remains the same, only the output of x+1 is compared with 0.