What is the difference between if(++x < 0){something}
and if(x + 1 < 0){something}
Thanks in advance
What is the difference between if(++x < 0){something}
and if(x + 1 < 0){something}
Thanks in advance
++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.
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
.