While converting a project from Python to C#, I found some interesting differences in the syntax families. Yet I got stuck, still unable to understand and comprehend the dissimilar behavior of comparison operator in C#.
During the course of curing this curiosity, I considered few languages of C-syntax family; C, C++, C#, Java, Javascript.. and verified the behavior. Here is how it transpired:
Let a=2, b=3, c=4, d=5;
Now, consider the following expressions:
a < a < a // returns true
c < b < a // returns true
c > b > a // returns false
a < c > b // returns false
If it was due to the right-associativity, then the following code in JavaScript shouldn't act like:
console.info(a < false); // returns false
console.info(a < a); // returns false
console.info(a < a < a); // returns true, as opposed to returning false
Here is the C/C++ version
int main(){
int a=2, b=3, c=4, d=5;
printf("%s\n","false\0true"+6*(a < a < a)); // returns true
printf("%s\n","false\0true"+6*(c < b < a)); // returns true
printf("%s\n","false\0true"+6*(c > b > a)); // returns false
printf("%s\n","false\0true"+6*(a < c > b)); // returns false
return 0;
}
Except in Python, where
a < a < a // returns false
c < b < a // returns false
c > b > a // returns true
a < c > b // returns true
Can anyone explain why C-family of languages and Python are computing the expressions differently?