我应该测试如果等于1或不等于0? [英] Should I test if equal to 1 or not equal to 0?
问题描述
我在这里编码的另一天,写了几个if语句与整数,总是 0
或 1
(实际上作为 bool
s)。我问自己:
I was coding here the other day, writing a couple of if statements with integers that are always either 0
or 1
(practically acting as bool
s). I asked myself:
当测试阳性结果,这是更好;测试 int == 1
或 int!= 0
?
When testing for positive result, which is better; testing for int == 1
or int != 0
?
例如,假设int n
,如果我想测试 true
code> n == 1 或 n!= 0
?
For example, given an int n
, if I want to test if it's true
, should I use n == 1
or n != 0
?
在速度,处理能力等方面是否有任何差别?
Is there any difference at all in regards to speed, processing power, etc?
请忽略这样的事实,即int可能多于/小于 1
推荐答案
人类的大脑更好地处理不包含否定的语句,这使得int == 1更好。
Human's brain better process statements that don't contain negations, which makes "int == 1" better way.
这篇关于我应该测试如果等于1或不等于0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!