By using OrElse
instead of Orin your code it allows a short circuit which allows the program to skip processes it doesn't require. When usingIf Statementsif expression1 isTruethen 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
(not evaluated)
True
False
True
True
False
False
False
As shown in my simple program If age1 is >=25 (True)
there is no need to evaluate age2 as the value returned is True
and it only needs one expression to be true to continue. If however age1 is <25 (False)
then it would evaluate age2 to determine if it is still possible to return a final value of True
, once again saving processing time.
This program will display a message box saying "Congratulations you may both enter the nightclub" if either person is over 25
Dim name1, name2 As String
Dim age1, age2 As Integer
name1 = InputBox("Please enter your name", "Name", "Max")
age1 = InputBox(name1 & " please enter your age:", "Age", 20)
name2 = InputBox("Plese enter your name", "Name", "Cindy")
age2 = InputBox(name2 & " please enter your age:", "Age", 25)
If age1 >= 25 OrElse age2 >= 25 Then
MessageBox.Show("Congratulations " & name1 & " and " & name2 & " you may both enter the nightclub!", "Enter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) Else MessageBox.Show("Sorry " & name1 & " and " & name2 & " you can't enter the nightclub", "Entry Denied", MessageBoxButtons.OK, MessageBoxIcon.Asterisk) End If
By using
OrElse
instead of
Orin your code it allows a short circuit which allows the program to skip processes it doesn't require. When usingIf Statementsif expression1 isTruethen it will not evaluate expression2 (as shown in table) which can save a lot of processing time.
expression1
is
expression2
is
As shown in my simple program If age1 is >=25
(True)
there is no need to evaluate age2 as the value returned is
True
and it only needs one expression to be true to continue. If however age1 is <25
(False)
then it would evaluate age2 to determine if it is still possible to return a final value of
True
, once again saving processing time.
This program will display a message box saying "Congratulations you may both enter the nightclub" if either person is over 25
Dim name1, name2 As String
Dim age1, age2 As Integer
name1 = InputBox("Please enter your name", "Name", "Max")
age1 = InputBox(name1 & " please enter your age:", "Age", 20)
name2 = InputBox("Plese enter your name", "Name", "Cindy")
age2 = InputBox(name2 & " please enter your age:", "Age", 25)
If age1 >= 25 OrElse age2 >= 25 Then
MessageBox.Show("Congratulations " & name1 & " and " & name2 & " you may both enter the nightclub!", "Enter", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Else
MessageBox.Show("Sorry " & name1 & " and " & name2 & " you can't enter the nightclub", "Entry Denied", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
End If