AndAlso


By using
AndAlso
instead of
Andin your code it allows a short circuit which allows the program to skip processes it doesn't require. When using
If Statementsif expression1 is Falsethen it will not evaluate expression2 (as shown in table) which can save a lot of processing time.


If
expression1
is
And
expression2
is
The value of return is
True
True
True
True
False
False
False
(not evaluated)
False

As shown in my simple example if name = FRED
(True)
then it will evaluate age. If name doesn't = FRED
(False)
then it will not evaluate age as it would be impossible to retun a final value of
True
, thus saving processing time.

This program will display a message box saying "Hello Fred you are of voting age!!" if the user is 18 or over and their name is Fred.

Dimname As String
Dim age As Integer

name = InputBox("Please enter your name", "Name", "John")
age = InputBox("Please enter your age", "Age", 20)

If name.ToUpper = "FRED" AndAlso age >= 18 Then
MessageBox.Show("Hello Fred you are of voting age!!", "Congratultaions", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
ElseIf name.ToUpper = "FRED" AndAlso age < 18 Then
MessageBox.Show("Hello Fred, sorry you are NOT of voting age!!", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Else
MessageBox.Show("Hello " & name & " you are not Fred!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If