In Python, and in some other languages, the expression
a < b < c is equivalent to
a < b and b < c. For example, the following code outputs “not increasing.”
a, b, c = 3, 1, 2 if a < b < c: print "increasing" else: print "not increasing"
Now consider the analogous C code.
int a = 3, b = 1, c = 2; if (a < b < c) printf( "increasingn" ); else printf("not increasingn");
This outputs “increasing”!
If you don’t know C, you’d expect the output to be “not increasing.” If you do know C, you might expect the code not to compile.
Here’s what’s going on. The code first asks whether
a < b. This evaluates to 0 because
a < b is false. Then it asks whether
0 < c, which is true.
If the code above were part of a C++ program, it would still compile and still produce the same result. However, the compiler would generate a warning for implicitly casting Boolean result to an integer.