Here is some “bad” code:
Module test
Sub Main()
Console.WriteLine("1<2 = " + cstr((1<2)))
Console.WriteLine("2<1 = " + cstr((2<1)))
Console.WriteLine("1<2<3 = " + cstr((1<2<3)))
Console.WriteLine("3<2<1 = " + cstr((3<2<1)))
End Sub
End Module
The output from this is:
1<2 = True
2<1 = False
1<2<3 = True
3<2<1 = True
1<2<3 is True, but not for the right reasons.
3<2<1 evaluates to True as well. Why?
Can someone explain what’s going on here?
I know I should be using a<b and b<c but I want to know what happens when you use consecutive operators. ie, why doesn’t the compiler cry!! Is syntax like this used for something else?
It evaluates it left to right, so
3<2<1is the same as(3<2)<1. Because expression in parentheses is false, the whole thing evaluates to0<1which is true.