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<1 is the same as (3<2)<1. Because expression in parentheses is false, the whole thing evaluates to 0<1 which is true.
First of all, with Option Strict On, the compiler does cry. With Option Strict Off, here's what happens:
3 < 2 < 1 is evaluated from left to right, so it's the equivalent to (3 < 2) < 13 < 2 is evaluated to False so the compiler evaluates: False < 1False to 0, so that it can be compared with another int value0 < 1 is evaluated to TrueIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With