What does this code do ?

Will the following VB code display True or False ?  Why ?

(and no cheating by actually trying the code and decompiling it)

 

Option Strict Off

 

Module Module1

 

   Sub Main()

 

      If 2 < 1 < 5 Then

         MsgBox(“true”)

      Else

         MsgBox(“false”)

      End If

   End Sub

 

End Module



6 Comments so far

  1.   Geoff Appleby on June 28th, 2006          

    Well, without running the code, i’d hazard my guess to be:

    2 < 1 = false False < 5 = true msgbox true. Now i'll run it and see what happens 🙂

  2.   Ayende Rahien on June 28th, 2006          

    2 < 1 == False False is either -1 (vb6) or 1 (VB.Net), and anyway this will make false < 5 true. I assume that setting option strict on will make the compiler complain, though.

  3.   bill on June 28th, 2006          

    d’oh, you guys are no fun 😉

    Yep, that’s pretty much the why. The what is that 2 < 1 < 5 is evaluated at compile time and the emmitted code is actually: It True Then MsgBox "true" Else MsgBox "false" End If

  4.   Jim Wooley on June 29th, 2006          

    Actually, the emitted code is Interaction.MsgBox(“true”, MsgBoxStyle.OkOnly, Nothing). The compiler optomizes the expression, determines it will always evaulate to true and simply replaces the expression with the end result as there are no variables present.

  5.   bill on June 29th, 2006          

    yep, in a release build. in a debug build that optimisations is not made, but the constant expression is still evaluated at compile time. So a Debug build looks like:
    It True Then
    MsgBox “true”
    Else
    MsgBox “false”
    End If

    Whereas a release build will further optimise that to:

    MsgBox “true”

  6.   John Mull on July 17th, 2006          

    I know that VB does this, particularly VB6. But why do the .Net versions assign an integer value to a boolean constant? Is this a hold-over from earlier versions of VB when there was no proper VB type?

    I just checked in Python and the expression does return True. The following code highlights that Python does the same integer equivocation with booleans:

    print False*10
    print True*10

    this returns:
    0
    10

    Therefore, in Python, False=0 and True=1.

    Wierd stuff. Maybe necessary to make the Truth Tables work out, but I’d just as soon see the booleans = False or True but not integers. I see the equation as being either:

    (2 < 1) = False False < 5 should return an error as we are comparing two different types, similar to stating that "A" < 5. -- or -- (2 < 1) = False False < 5 = False since the only true equation involving False could be False = False. Admittedly, the former method seems the most logical to me. I caught the order of operations just fine, but the underlying integer value of a boolean seems a little awry. My fav scripting language, Python, does the same, so I can't claim language superiority on this one. *sigh* Any there really good explanations for why False and True have underlying integer values? -- John Mull Programmer/Analyst Catawba County Government Newton, North Carolina, USA johnm@catawbacountync.gov