问题
I have a simple bit of logic.
int i = 0;
if (i < 0) {
//whatever;
}
When I debug with VS I see i
set to 0 BUT the if evaluates as false! How can this be?
GUYS. Perhaps I could have worded it better!!! The above expression SHOULD evaluate as false when i is 0 which I see when I hover over it in VS BUT it goes into the brackets and does "whatever"... WHICH is not right.
EDIT: Please see my similarly named but more recent question for a solution.
回答1:
0 is not less than 0. Its equal to. Do if (i <= 0)
回答2:
That's because 0 < 0 is false.
回答3:
Because i is not less than zero. So, the expression evaluates as false, which is correct.
回答4:
0 is NOT less than 0. it is less than OR EQUAL to 0
回答5:
It evaluates to false because 0 is not less than 0.
回答6:
The answer is because i isn't less than 0.
In order for the if statement to evaluate to true, i would need to be a negative integer.
回答7:
If i
equals 0, then it is false, because it is not less than 0.
What you are thinking of is if(i == 0)
or if (i <= 0)
(or for that matter if(i >= 0)
). Each of these are true if i
equals 0
.
回答8:
0 < 0
will always be false
. Under what condition do you expect it to evaluate to true
?
回答9:
0 < 0
is false. use <=
if you want it to evaluate to true if i
is 0
回答10:
Maybe your simple bit of logic needs to be:
int i = 0;
if(i <= 0) {Whatever }
来源:https://stackoverflow.com/questions/6389261/how-can-if-evaluate-incorrectly-in-c-sharp