You've already got a lot of answers, but I'll add one more to cover one other possible point of confusion.
In C & Obj-C the boolean (and character) types are treated as integer types, which is not the case in call languages. So expressions like 'z' * true
make perfect sense!
(Modern) C uses the type _Bool
for boolean, which is defined to be large enough to hold 0
& 1
. Cocoa uses the type BOOL
for boolean, which is defined as signed char
. CoreFoundation uses the type Boolean
which is defined as unsigned char
. All three define YES
/true
as 1
and NO
/false
as 0
, while C itself treats any non-zero value as true
.
The relation operators such as <
, <=
etc. are defined to return the int
(yes, none of the booleans, not even _Bool
) value 0
if the relation is false, and the int
value 1
if the relation is true.
Given this and the left-to-right associativity of relational operators your:
if (100 <= x <= 149)
is parsed as:
if ((100 <= x) <= 149)
then 100 <= x
evaluates to the int
value 1
if x
is greater than or equal to 100, otherwise it evaluates to the int
value 0
, so we get:
if (1 <= 149)
or
if (0 <= 149)
both of these evaluate to 1
so we get:
if (1)
and the if
statement branches to the "then" branch if it's expression is non-zero.
It may be surprising, but the whole statement is evaluated without any use of booleans at all - it is all done with integers.
To achieve what you intended you need:
if((100 <= x) && (x <= 149))
etc. - which also doesn't use any booleans (&&
is defined in terms of integers).