Question

For the following If-statements in VB.NET, what will be the sequence in which the conditions will be evaluated?

Case 1:

If ( condition1 AND condition2 AND condition3 )
.
.
End If

Case 2:

If ( condition1 OR condition2 OR condition3 )
.
.
End If

Case 3:

If ( condition1 OR condition2 AND condition3  OR condition4)
.
.
End If
Was it helpful?

Solution

VB.NET is a very strange beast from the C-programmer standpoint. As Gerrie mentioned in a different answer, all three conditions are evaluated in their full entirety without short-circuiting. AndAlso and OrElse can save your day if that's what you want.

As for the last if, the order of evaluation is as follows:

If ((condition1 OR (condition2 AND condition3))  OR condition4)

As a rule of thumb: if there's any ambiguitiy, use brackets to specify order of evaluation explicitly.

OTHER TIPS

VB.Net evaluates all the conditions, so the order isn't important here.

If you want to use short-circuiting, use the AndAlso and OrElse keywords.

This is not exactly answering the question at hand, as it was already answered, I felt the need to expand on the 'OrElse' keyword, mentioned by Anton, as well as it's relative: 'AndAlso', because someone landing on this question my actually want an explanation on these.

'OrElse' and 'AndAlso' Is useful if you require an explicitly ordered evaluation.

Unlike 'Or' and 'And', were all the expressions are evaluated, the If statement can skip the remaining expressions if the first expression evaluates to the desired value, when you make use of 'OrElse' or 'AndAlso'.

In the following case, order of the expressions are applicable to 'OrElse' from Left to Right, but also does not always evaluate all the expressions depending on the result of the preceding value :

if( Expression1 orelse Expression2 orelse Expression3 )

    ' Code...

End If

If Expression1 is true, then Expression 2 and 3 are ignored. If Expression1 if False, then Expression2 is evaluated, and then Expression3 if Expression2 evaluates to True.

If (False OrElse True OrElse False ) then
    ' Expression 1 and 2 are evaluated, Expression 3 is ignored.
End If

If (True OrElse True OrElse True ) then
    ' only Expression 1 is evaluated.
End If

If (True OrElse True OrElse True ) then
    ' only Expression 1 is evaluated.
End If

If (False OrElse False OrElse True ) then
    ' All three expressions are evaluated
End If

An alternate example of the 'OrElse' usage:

If( (Object1 is Nothing) OrElse (Object2 is Nothing) OrElse (Object3 is Nothing) ) then 


    ' At least one of the 3 objects is not null, but we dont know which one.
    ' It is possible that all three objects evaluated to null.
    ' If the first object is null, the remaining objects are not evaluated.
    ' if Object1 is not  NULL and Object2 is null then Object3 is not evaluated

Else

   ' None of the objects evaluated to null.

Endif

The above example is the same as the following:

If(Object1 Is Nothing)

    ' Object 1 is Nothing
    Return True

Else 
    If(Object2 Is Nothing)
        ' Object 2 is Nothing 
        Return True
    Else
        If(Object3 Is Nothing)
            ' Object 3 is Nothing 
            Return True
        Else
            ' One of the objects evaluate to null
            Return False
        End If    
    End If      
End If 

There is also the AndALso keyword:

' If the first expression evaluates to false, the remaining two expressions are ignored
If( Expression1 AndAlso Expression2 AndAlso Expression3 ) Then ...

Which is usable like this:

If( (Not MyObject is Nothing) AndAlso MyObject.Enabled ) Then ...

   ' The above will not evaluate the 'MyObject.Enabled' if 'MyObject is null (Nothing)'
   ' MyObject.Enabled will ONLY be evaluated if MyObject is not Null.

   ' If we are here, the object is NOT null, and it's Enabled property evaluated to true

Else

  ' If we are here, it is because either the object is null, or it's enabled property evaluated to false;

End If 

Unlike 'And', like 'Or'- will always evaluate all expressions:

If( (Not MyObject is Nothing) And MyObject.Enabled ) Then ...

   ' MyObject.Enabled will ALWAYS be evaluated, even if MyObject is NULL,
   ' ---  which will cause an Exception to be thrown if MyObject is Null.

End If 

But because order can have an effect with 'OrElse' and 'AndAlso', evaluating weather an object is null should be done first, as in the above examples.

The following will cause an exception if 'MyObject' is NUll

Try 
    If( MyObject.Enabled  AndAlso (Not MyObject is Nothing) ) Then ...

       ' This means that first expression MyObject.Enabled Was evaluated  to True, 
       ' Second Expression also Evaluated to True

    Else

       ' This means MyObject.Enabled evaluated to False, thus also meaning the object is not null.
       ' Second Expression "Not MyObject is Nothing" was not evaluated.

    End If 
Catch(e as Exception)

    ' An exception was caused because we attempted to evaluate MyObject.Enabled while MyObject is Nothing, before evaluating Null check against the object.

End Try

DO please comment if I made a mistake or typo here, as I wrote this at 5:16AM in the morning after 2 days without sleep.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top