People who write a code that is accustomed to C + + should be accustomed to seeing code like this:
| 12345 |
inti = 0;......if(i){ //这里代码不会被执行} |
Therefore, the writing habit will assume that the other language should be 0 in the IF condition also represents the false value. Except for a few very strong types that require if the language after the IF must be bool (c#,f# ...) Other languages that allow the other values to be placed in the IF condition should be 0 to indicate false, and not 0 for true.
But this is not the case, what value represents true, what value represents false, is actually related to language design concepts and logic, such as C such as hardware close to and very pursuit of performance of the language, with an int to indicate that the Boolean value is more performance (including compilation performance) considerations, and if the integer represents bool, It is clear that 0 is false. and some languages derived from C, although the level of abstraction may be higher, the reason for this to be false for 0 does not exist, but because of the compatibility requirements, you will still choose to have 0 values of false, not 0 values of true, such as Python.
However, some other languages are not so, such as a large number of Lisp language, many think "as long as the value is true, there is no value is false" that is, only null/none/nil/empty/missing/undefined/ Unknown semantics should be false, and other deterministic values should be true. Therefore 0 should not be the exception, 0 is true, for example, for Clojure, only the null value (nil) and false itself is false, and any other value is true. And for the scheme language, more strict, even empty table and Null values are counted true, in the scheme language, in addition to a #f to denote false semantics, the other "Anything" is true semantics, including empty!
So in scheme and Clojure and many other Lisp dialects, the value of (if 0 1 2) is 1 instead of 2. (this if expression is similar to the C language (0-1:2))
Lisp abstraction is very high, semantics is more important than performance, so 0 value as false and no real meaning, depending on the language, false semantics may be empty, perhaps it is really just "false", the understanding of the meaning and requirements of different, so 0 nature is not always false.
"0" is not necessarily false (false)