3

Possible Duplicate:
Should I always use the AndAlso and OrElse operators?

In a practical implementation for all scenarios, the logical expression will return the same boolean value for both And and AndAlso,

why not we use always AndAlso and save the processing time for the next condition when the previous is false? In other words, Why And is still in use?

Community
  • 1
  • 1
Alaa Alweish
  • 8,904
  • 16
  • 57
  • 84
  • since 99.99% of uses should be `AndAlso`, why not the `AndAlso` becomes `And` and the `And` becomes another keyword? – Alaa Alweish Dec 08 '12 at 22:43

2 Answers2

5

The And operator in VB.NET performs two jobs, it is both a logical and a mathematical operator. Early versions of Visual Basic did not distinguish between the two. By design, the language was designed to be easy-to-use and forcing Visual Basic programmers to learn the difference was something the language designers wanted to avoid.

That worked pretty well, although it gave the language a few quirks. The value of True for example is not 1, like it is in many languages, it is -1. The value you get from CInt(True). Which allows ignoring the difference between the two uses of the operator, it still works well when an If() statement uses And when the left side is an Integer and the right side is a Boolean for example.

But there's a pretty specific problem with the And operator playing both roles. A common usage is to write a statement like this:

If ix < array.Length And array(ix) <> 42 Then
   '' do something
End If

That's a statement that will make your code crash with an IndexOutOfRangeException. What you meant is "if the index is out of bounds then don't bother checking the array element". That's called "short-circuit evaluation". But that's not what the And operator does, it evaluates both the left and the right expressions. Like a mathematical version of the And operator must do.

Short-circuit evaluation is important and has been around a long time. And universally adopted in the curly-brace languages, starting with C. And it finally got adopted in VB.NET as well, you write it like this to avoid the exception:

If ix < array.Length AndAlso array(ix) <> 42 Then
   '' do something
End If

So to answer your question: Yes, always use AndAlso when you meant to use the logical version of the And operator. AndAlso is the logical version.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Isn't `AndAlso` slower than `And` as a logical operator? It must be adding some `If...Then` and `GoTo` logic behind the scenes. – Victor Zakharov Dec 09 '12 at 00:56
  • 1
    @Neolisk: In general assume `AndAlso` is *faster* than `And` - imagine this: `If AliensExist () And WaitForAliens () Then ...` - this will block until aliens arrive, even if aliens don't exist. With AndAlso it would not block if aliens don't exist. – Rolf Bjarne Kvinge Dec 10 '12 at 14:03
  • @RolfBjarneKvinge: what about `If a = 5 AndAlso b = 7` vs `If a = 5 And b = 7`? – Victor Zakharov Dec 10 '12 at 14:33
  • @Neolisk: most likely the JIT would optimize it to the same code. And in any case if there were a difference you'd have to run it millions of times in a loop to detect it. – Rolf Bjarne Kvinge Dec 10 '12 at 21:58
1

Sometimes you want both pieces to be executed, for side effects. Not everything is a pure function. Also: I'm assuming we're talking about booleans, but in the case of integers etc And means bitwise ops.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900