114

What's the difference between or and OrElse?

if temp is dbnull.value or temp = 0

produces the error:

Operator '=' is not defined for type 'DBNull' and type 'Integer'.

while this one works like a charm!?

if temp is dbnull.value OrElse temp = 0

8 Answers 8

169

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.

OrElse knows this, so doesn't try and evaluate temp = 0 once it's established that temp Is DBNull.Value

Or doesn't know this, and will always attempt to evaluate both terms. When temp Is DBNull.Value, it can't be compared to zero, so it falls over.

You should use... well, whichever one makes sense.

9
  • 3
    So Or only makes sense when I call a function after the or that has side effects my code depends on? Commented Jul 23, 2009 at 10:09
  • 4
    Or makes sense in all cases where the second item does not trigger error if the first one is true...
    – awe
    Commented Jul 23, 2009 at 10:16
  • 4
    @ malach: I suppose so (you really get OrElse behaviour as default in most other languages): It's not a good idea to call functions with side effects in compound conditionals it makes the code unreadable.
    – Utaal
    Commented Jul 23, 2009 at 10:16
  • 5
    @ awe: yeah, but why do you even want to waste time evaluating something which by definition won't change the result of the expression?
    – Utaal
    Commented Jul 23, 2009 at 10:18
  • 3
    @MarkJ: I don't really think four extra characters disrupt readability. On the other hand, using an operator that depends on the existence of side-effects (as malach wrote) to be meaningful sounds like a bad idea (and can make readability harder!). I would consider side-effects in such places a big no-no, and can not think of any situations in which I would prefer "Or" to "OrElse". It's a pity that these operators work this way, as the "OrElse" behaviour is probably what most expect even when using "Or" (especially when coming from other languages).
    – Kjartan
    Commented Aug 13, 2012 at 13:33
48

This is the same behaviour as with C#, where everyone uses the Coditional Or (||) and the Conditional And (&&), where you also have the normal Or (|) and normal And (&). So comparing C# to VB.Net is:

| => Or

|| => OrElse

& => And

&& => AndAlso

The condifitonal boolean operators are very usefull preventing nested if constructions. But sometimes the normal boolean operators are needed to ensure hitting both code paths.

2
  • 11
    I never actually knew this was available. Thanks for new information. Good to know, even though I can't really see any situation in which I would want to use "|". I think it would require the second condition to cause side-effects to make any sense, and that in itself makes little sense in my opinion! ;)
    – Kjartan
    Commented Aug 13, 2012 at 13:20
  • 10
    Eh, as far as I know, | and & are bitwise operators in C#, not boolean operations at all.
    – Nyerguds
    Commented Aug 5, 2016 at 13:01
12

OrElse is short circuited, this means that only one side of the expression will be tested if the first side is a match.

Just like AndAlso will only test one side of the expression if the first half is a fail.

8

OrElse evaluate first expression then if its true it will proceed to the statement while OR evaluates two expressions before it will proceed to their statement.

Example:

Textbox1.Text= 4

Textbox2.Text= ""

Using OrElse

  If TextBox1.Text > 2 OrElse TextBox2.Text > 3 Then
      MsgBox("True")
  End If

Result is: TRUE


Using OR

 If TextBox1.Text > 2 Or TextBox2.Text > 3 Then

            MsgBox("True")
  End If

Result is: Error cannot convert string to double.

6

(I've looked at other answers and realized I was terribly wrong)

The OrElse operator "performs short-circuiting logical disjunction on two expressions", that is to say: if the left operand is true and so the entire expression is guaranteed to be true the right operand won't even be evaluated (this is useful in cases like:

string a;
//...
if (a is null) or (a = "Hi") //...

to avoid a NullReferenceException throw by the right-hand operand.

I'm sincerely astonished that this (lazy evaluation) isn't the default behaviour of or and and as it is in C/C++ and C# (and many other languages...)

3
  • 8
    The thing is, in VB classic, there was just And and Or, which were non-short-circuiting. I think I'm right in saying that the first betas of VB.NET actually changed the behaviour of these operators - there was uproar, so they were changed back and AndAlso and OrElse (short-circuiting) were introduced. I can only imagine the alternative names they must have considered if these were the ones that were the best...
    – AakashM
    Commented Jul 23, 2009 at 10:20
  • 1
    By providing Or and OrElse (| and || in C#) this allows the developer to choose how they handle their own code. Using the code above, I'd have to use a try catch around it to handle a null value in the variable a. OrElse allows the developer to handle this in the else of the if statement as a known possible outcome rather than an exception. This is more obvious if the variable a was a parameter in a method, where you have less control over when the variable is assigned a value (i.e. outside the method)
    – Kevin Hogg
    Commented Mar 22, 2012 at 14:54
  • Short circuiting is not the default behavior of OR and AND in c#, either. c# has two different operators for bitwise and logical/short-circuiting operations. && and || perform logical comparisons, and return a boolean value The & and | operators are bitwise and return an integer value. So where 1 || 2 returns "true", 1 | 2 returns "3".
    – TomXP411
    Commented Dec 20, 2018 at 18:27
3

The Bert' s answer is not very accurate. The '|' or '&' is logical operator, in C #, it always treat as bit operator, please see the following code as example

        static void Main()
        {
            object a = null;
            int b = 3;
            if (a == null | a.ToString() == "sdffd")
            {
                Console.WriteLine("dddd");
            }
            Console.WriteLine(b | b);
            Console.Read();
        }

The following is IL

    .method private hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       62 (0x3e)
  .maxstack  3
  .locals init ([0] object a,
           [1] int32 b,
           [2] bool CS$4$0000)
   IL_0000:  nop
   IL_0001:  ldnull
   IL_0002:  stloc.0
   IL_0003:  ldc.i4.3
   IL_0004:  stloc.1
   IL_0005:  ldloc.0
   IL_0006:  ldnull
   IL_0007:  ceq
   IL_0009:  ldloc.0
   IL_000a:  callvirt   instance string [mscorlib]System.Object::ToString()
   IL_000f:  ldstr      "sdffd"
   IL_0014:  call       bool [mscorlib]System.String::op_Equality(string,
                                                                 string)
   IL_0019:  or
   IL_001a:  ldc.i4.0
   IL_001b:  ceq
   IL_001d:  stloc.2
   IL_001e:  ldloc.2
   IL_001f:  brtrue.s   IL_002e
   IL_0021:  nop
   IL_0022:  ldstr      "dddd"
   IL_0027:  call       void [mscorlib]System.Console::WriteLine(string)
   IL_002c:  nop
   IL_002d:  nop
   IL_002e:  ldloc.1
   IL_002f:  ldloc.1
   IL_0030:  or
   IL_0031:  call       void [mscorlib]System.Console::WriteLine(int32)
   IL_0036:  nop
   IL_0037:  call       int32 [mscorlib]System.Console::Read()
   IL_003c:  pop
   IL_003d:  ret
    } // end of method Program::Main

when you use || to test "a == null" and "a.ToString() == "sdffd", the IL will be

 .method private hidebysig static void  Main() cil managed
{
  .entrypoint
  // Code size       63 (0x3f)
  .maxstack  2
  .locals init ([0] object a,
           [1] int32 b,
           [2] bool CS$4$0000)
  IL_0000:  nop
  IL_0001:  ldnull
  IL_0002:  stloc.0
  IL_0003:  ldc.i4.3
  IL_0004:  stloc.1
  IL_0005:  ldloc.0
  IL_0006:  brfalse.s  IL_001d
  IL_0008:  ldloc.0
  IL_0009:  callvirt   instance string [mscorlib]System.Object::ToString()
  IL_000e:  ldstr      "sdffd"
  IL_0013:  call       bool [mscorlib]System.String::op_Equality(string,
                                                                 string)
  IL_0018:  ldc.i4.0
  IL_0019:  ceq
  IL_001b:  br.s       IL_001e
  IL_001d:  ldc.i4.0
  IL_001e:  stloc.2
  IL_001f:  ldloc.2
  IL_0020:  brtrue.s   IL_002f
  IL_0022:  nop
  IL_0023:  ldstr      "dddd"
  IL_0028:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_002d:  nop
  IL_002e:  nop
  IL_002f:  ldloc.1
  IL_0030:  ldloc.1
  IL_0031:  or
  IL_0032:  call       void [mscorlib]System.Console::WriteLine(int32)
  IL_0037:  nop
  IL_0038:  call       int32 [mscorlib]System.Console::Read()
  IL_003d:  pop
  IL_003e:  ret
} // end of method Program::Main

Now you can see the difference, please don't think the '|' or 'and' as conditional operator, it just a logical operator, I don't think there is necessary to use it to judge condition

2
  • 2
    The '|' or '&' is logical operator, in C #, it always treat as bit operator. I believed in this too, until I saw this reference, msdn.microsoft.com/en-us/library/kxszd0kx.aspx Commented Nov 4, 2016 at 19:53
  • Your answer is out of context. The question does not relate to C# but rather to VB where four logical operators: And, AndAlso, Or, OrElse, Not, and Xor are both, logical and Bitwise operators. Commented May 9, 2019 at 16:50
1

The reason the compilation fails in the example is the order of operations.

The expression parser is trying to evaluate "dbnull.value or temp" first.

if temp is (dbnull.value or temp) = 0

The error is here, because you can't do a bitwise OR between an integer (temp) and dbnull.value.

OrElse fixes this, not because it's short-circuited, but because it's lower on the order of operations, and so "temp is dbnull.value" and "3=0" are being evaluated first, rather than the parser trying to compare dbNull and temp.

So the evaluation with OrElse works like you're expecting: (assume temp=3)

if temp is dbnull.value OrElse temp = 0 then
if 3 is dbnull.value OrElse 3 = 0 then
if false OrElse 3=0 then
if false OrElse false then
if false then

This was actually on an entry exam at a software company I used to work for, and it was a common problem I used to encounter in VB6. So it's a good idea to parenthesize your sub-expressions when using boolean operators:

This would have compiled properly:

if (temp is dbnull.value) Or (temp = 0) then 

Although, as everyone has already pointed out, OrElse and AndAlso are really the correct operators to use in this context.

2
  • If this were true, the exception would be "Operator 'Or' is not defined for type 'DBNull' and type 'DBNull'. As this is not the case, we can see it has nothing to do with order of operations. Note, the operator precedence for vb.net is listed as "is" and "=" being equal, so those are left to right. "Or" and "OrElse" are after, but equal to one-another, so are evaluated after the "is" (and "=" only in the case of "OR") source: learn.microsoft.com/en-us/dotnet/visual-basic/…
    – Erick P
    Commented Jun 20, 2023 at 18:20
  • Considering I've seen this exact error in code I've worked on, more than once, and grouping the terms with parentheses fixes it every time, I'm sticking with my answer as the most likely in this situation. I know what the docs say now, but it wouldn't be the first time this has changed. I've seen the docs be not entirely consistent with observed behavior in the past, and the surest way to make the code work as expected is parentheses around the "is" and the equal clauses.
    – TomXP411
    Commented Jun 21, 2023 at 22:40
0

Unless your code logic requires the short-circuiting behavior OrElse provides, I would lean toward using the Or operator because:

  • Using "Or" is simple and requires less typing.
  • The computational time savings of using OrElse is negligible in most cases.
  • Most importantly, using OrElse can hide errors in later clauses that may not be initially revealed until those conditions would eventually be met by the program logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.