{"id":95,"date":"2011-04-11T20:23:00","date_gmt":"2011-04-12T01:23:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=95"},"modified":"2023-07-26T16:02:24","modified_gmt":"2023-07-26T21:02:24","slug":"visual-basic-operators-andalso-orelse","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=95","title":{"rendered":"Visual Basic Operators:  &#8216;AndAlso&#8217; &#038; &#8216;OrElse&#8217;"},"content":{"rendered":"<p>Since I&#8217;ve mostly been using C# for the past few years, I sometimes get confused when I come across a Visual Basic application and have to distinguish between the newer operators (OrElse, AndALso vs Or, And)<\/p>\n<p>Operator: OrElse <\/p>\n<p>This example uses the OrElse operator to perform logical disjunction on two expressions. The result is a Boolean value that represents whether either of the two expressions is true. If the first expression is True, the second is not evaluated.  If the first expression is false then the second expression is evaluated.<\/p>\n<p>Dim A As Integer = 10<br \/>Dim B As Integer = 8<br \/>Dim C As Integer = 6<br \/>Dim myCheck As Boolean<br \/>myCheck = A &gt; B OrElse B &gt; C   &#8216; True. Second expression is not evaluated.<br \/>myCheck = B &gt; A OrElse B &gt; C   &#8216; True. Second expression is evaluated.<br \/>myCheck = B &gt; A OrElse C &gt; B   &#8216; False.<\/p>\n<p>Operator:  Or  (if either expression evaluates to true then true will be returned)<\/p>\n<p>Dim A As Integer = 10<br \/>Dim B As Integer = 8<br \/>Dim C As Integer = 6<br \/>Dim myCheck As Boolean<br \/>myCheck = A &gt; B Or B &gt; C   &#8216; Returns True.<br \/>myCheck = B &gt; A Or B &gt; C   &#8216; Returns True.<br \/>myCheck = B &gt; A Or C &gt; B   &#8216; Returns False.<\/p>\n<p>Operator:  AndAlso (if first expression evaluates to false then second expression is not evaluated)<\/p>\n<p>Dim a As Integer = 10<br \/>Dim b As Integer = 8<br \/>Dim c As Integer = 6<br \/>Dim firstCheck, secondCheck, thirdCheck As Boolean<br \/>firstCheck = a &gt; b AndAlso b &gt; c   &#8216; Returns True.<br \/>secondCheck = b &gt; a AndAlso b &gt; c  &#8216; Returns False<br \/>thirdCheck = a &gt; b AndAlso c &gt; b   &#8216; Returns False<\/p>\n<p>Operator:  And  (both expressions are evaluated)<\/p>\n<p>Dim a As Integer = 10<br \/>Dim b As Integer = 8<br \/>Dim c As Integer = 6<br \/>Dim firstCheck, secondCheck As Boolean<br \/>firstCheck = a &gt; b And b &gt; c   &#8216; Returns True.<br \/>secondCheck = b &gt; a And b &gt; c  &#8216; Returns False<\/p>\n<p>Ideally,  the short circuit operations, you&#8217;re saving on the extra condition check when the first one is satisfied (OrElse) or not satisfied (AndAlso). So these should give you better performance in general.<\/p>\n<p>OrElse: <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ea1sssb2.aspx\" target=\"_blank\" rel=\"noopener\"> http:\/\/msdn.microsoft.com\/en-us\/library\/ea1sssb2.aspx<\/a><br \/>AndAlso:<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cb8x3kfz.aspx\" target=\"_blank\" rel=\"noopener\"> http:\/\/msdn.microsoft.com\/en-us\/library\/cb8x3kfz.aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I&#8217;ve mostly been using C# for the past few years, I sometimes get confused when I come across a Visual Basic application and have to distinguish between the newer operators (OrElse, AndALso vs Or, And) Operator: OrElse This example uses the OrElse operator to perform logical disjunction on two expressions. The result is a &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=95\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Visual Basic Operators:  &#8216;AndAlso&#8217; &#038; &#8216;OrElse&#8217;&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[86],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/95"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=95"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":252,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions\/252"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}