AndAlsoare two new logical operators in VB .NET and they have some properties that can enhance your codes in two general categories:
1. Avoid executing part of a logical expression.
2. Optimize code by not executing any more of a compound expression than required.
AndAlsoare quite similar with the
Orexcept that VB .NET supports short-circuiting with the
AndAlsooperators. This means that the expressions will only be executed when necessary. Anyway, the
Orare still present in VB .NET.
// Assume that str1 = 5,
// x = 1 and y = 1
If x > str1 And y < str1 Then
When performing an
Andoperator in VB .NET, it actually evaluates both of the expressions to get the final outcome. Even the first condition (
x greater than str1) returns as
FALSE, it still continues to look at the second argument even though it doesn’t need to.
Let’s see how
AndAlsoevaluates the codes below.
If x > str1 AndAlso y < str1 Then
AndAlso, VB .NET knows that the expression will not succeed once it is determined that the first condition (
x greater than str1) is
FALSE. So it stops evaluating the expression right away without checking the second condition.
The difference of
OrElseare also similar to
Orwill check all the conditions and
OrElsewon’t checking the remaining expression, if it found any of the previous condition is