Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(OrElse and Or) and (AndAlso and And) - When to use?

People also ask

What is difference between and and AndAlso in Uipath?

This is used to connect two (or more) logical conditions checking like if (condition1 AndAlso condition2). If the first condition is false, It won't perform the second condition . So our execution time is saving in a logical operation in which more conditions are combined using “AndAlso" operator.

How is the AndAlso operator different from the and operator?

The And operator evaluates both sides, where AndAlso evaluates the right side if and only if the left side is true.

What is the difference between OR and OrElse in VB net?

OrElse is a short-circuiting operator, Or is not. By the definition of the boolean 'or' operator, if the first term is True then the whole is definitely true - so we don't need to evaluate the second term. Or doesn't know this, and will always attempt to evaluate both terms.

What does AndAlso operator do?

Data Types. The AndAlso operator is defined only for the Boolean Data Type. Visual Basic converts each operand as necessary to Boolean before evaluating the expression. If you assign the result to a numeric type, Visual Basic converts it from Boolean to that type such that False becomes 0 and True becomes -1 .


Or/And will always evaluate both1 the expressions and then return a result. They are not short-circuiting.

OrElse/AndAlso are short-circuiting. The right expression is only evaluated if the outcome cannot be determined from the evaluation of the left expression alone. (That means: OrElse will only evaluate the right expression if the left expression is false, and AndAlso will only evaluate the right expression if the left expression is true.)

Assuming that no side effects occur in the expressions and the expressions are not dependent (and any execution overhead is ignored), then they are the same.

However, in many cases it is that the expressions are dependent. For instance, we want to do something when a List is not-Nothing and has more than one element:

If list IsNot Nothing AndAlso list.Length > 0 Then .. 'list has stuff

This can also be used to avoid an "expensive" computation (or side-effects, ick!):

If Not Validate(x) OrElse Not ExpensiveValidate(x) Then .. 'not valid

Personally, I find that AndAlso and OrElse are the correct operators to use in all but the 1% - or less, hopefully! - of the cases where a side-effect is desired.

Happy coding.


1 An Exception thrown in the first expression will prevent the second expression from being evaluated, but this should hardly be surprising ..


Besides the short-circuiting mentioned in the other answers, Or/And are usable as bitwise operators where OrElse/AndAlso are not. Bitwise operations include combining values of Flags enums, such as the FileAttributes enumeration where you might indicate a file is both read only and hidden by FileAttributes.ReadOnly Or FileAttributes.Hidden


The difference is that OrElse and AndAlso will short-circuit based on the first condition, meaning that if the first condition doesn't pass, the second (or more) conditions will not be evaluated. This is particularly useful when one of the conditions might be more intensive than the other.

Example where Or is fine (both conditions evaluated):

If Name = "Fred" Or Name = "Sam" Then

It really doesn't matter which way around they are evaluated

The following AndAlso is useful because the second condition might fail

If Not SomeObject Is Nothing AndAlso CheckObjectExistsInDatabase(SomeObject) Then

This allows for the first condition to check whether the object has been set and only if it has been set will go and check the database (or some other task). If this had been a plain And keyword, both would be evaluated.


@Gideon - glad someone pointed that out. Here is a simple test that shows the dramatic impact of AndAlso:

    Dim tm As New Stopwatch
    Const tries As Integer = 123456
    Dim z As Integer = 0
    Dim s() As String = New String() {"0", "one"}

    Debug.WriteLine("AndAlso")
    For x As Integer = 0 To s.Length - 1
        z = 0
        tm.Restart() 'restart the stopwatch
        For y As Integer = 0 To tries
            If s(x) = x.ToString AndAlso s(x) = y.ToString Then '<<<<<<<<<<
                z += 1
            End If
        Next
        tm.Stop()
        Debug.WriteLine(x.ToString.PadRight(3, " "c) & z.ToString.PadRight(10, " "c) & tm.Elapsed.ToString)
    Next

    Debug.WriteLine("And")
    For x As Integer = 0 To s.Length - 1
        z = 0
        tm.Restart() 'restart the stopwatch
        For y As Integer = 0 To tries
            If s(x) = x.ToString And s(x) = y.ToString Then '<<<<<<<<<<
                z += 1
            End If
        Next
        tm.Stop()
        Debug.WriteLine(x.ToString.PadRight(3, " "c) & z.ToString.PadRight(10, " "c) & tm.Elapsed.ToString)
    Next