How can if() evaluate incorrectly in C# [closed]

别等时光非礼了梦想. 提交于 2019-12-13 10:12:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!