Keyword Array Collections File String Cast Class Console Data Dictionary Enum Exception For If Interface Lambda LINQ List Nothing Number Process Property Regex Select Sort Split StringBuilder Structure Sub Time Windows
with an If-statement,
we direct control flow. The condition is evaluated. On a true result, control moves to the statements inside the block. Multiple expressions are short-circuited.
An If-statement uses the If-keyword and the Then-keyword. The ElseIf-statement has the "If" part capitalized in the middle. And the Else-statement has no "Then" part. We end with an "End If" statement.
Equals:We do not use two equals signs together in an If-statement. We just use one.
Tip:Visual Studio will automatically insert the "Then" and "End If" parts of an If-statement.
Based on: .NET 4.5 VB.NET program that uses If, ElseIf and Else Module Module1 Sub Main() ' Get input. Dim s As String = Console.ReadLine() ' Check input with If, Elseif and Else. If s = "cat" Then Console.WriteLine("You like cats") ElseIf s = "dog" Then Console.WriteLine("You like dogs") Else Console.WriteLine("No choice made") End If End Sub End Module Output dog You like dogs
An If-statement can also test a negative. Instead we must apply the Not-keyword. The If-statement then reads "If not this, then." We also can apply the Not-keyword to the ElseIf clause.
Note:We cannot use the "!=" operator. We must apply the Not-operator and then an equals expression.
VB.NET program that uses If Not Module Module1 Sub Main() ' An integer variable. Dim i As Integer = 100 ' Test integer against 100 and 200. If Not i = 100 Then Console.WriteLine("i not 100") ElseIf Not i = 200 Then Console.WriteLine("i not 2") End If End Sub End Module Output i not 2
With the "And"
and "Or" keywords,
we test complex expressions. These expressions are chained. If an expression with And, the first test that fails will cause the expression to be stopped.
And:In the Or case, the first test that succeeds will cause the expression to be stopped (short-circuited).
Tip:To use these binary (two-part) operators, we use the English words. This is a different syntax than "&&" and "||".
VB.NET program that uses And, Or Module Module1 Sub Main() Dim left As Integer = 10 Dim right As Integer = 100 ' Use "and" in expression. If left = 10 And right = 100 Then Console.WriteLine(1) End If ' Use "or" in expression. If left = 5 Or right = 100 Then Console.WriteLine(2) End If End Sub End Module Output 1 2
An If-statement does not always directly test values. Sometimes it calls another Function and tests the value returned by that Function. In this example, the If-statement calls the IsValid Function.Function
Then:The IsValid Function is evaluated. It returns True if the Integer argument is within a certain range.Boolean
So:If the Integer is within the specified range (10 to 100 inclusive) the inner block of the If-statement is reached.
VB.NET program that uses Function, If-statement Module Module1 ''' <summary> ''' See if size is valid. ''' </summary> Function IsValid(ByVal size As Integer) As Boolean ' Returns true if size is within this range. Return size >= 10 And size <= 100 End Function Sub Main() ' Size variable. Dim size As Integer = 50 ' Call IsValid function in an If-expression. If IsValid(size) Then Console.WriteLine("Valid size") End If End Sub End Module Output Valid size
If-statements can become complex and hard to understand. It is sometimes possible to simplify them by adding local variables. Here we introduce a Boolean that stores whether the animal and size variables indicate a "big cat."
Then:We can test the isBigCat variable in the two If-statements. This reduces code repetition and makes the program easier to read.
Also:In some programs, we can use this approach to reduce expensive method calls by storing their results for later reuse.
VB.NET program that tests local variable Module Module1 Sub Main() Dim animal As String = "cat" Dim size As Integer = 10 Dim color As String = "grey" ' Store expression result in local variable. Dim isBigCat As Boolean = (animal = "cat" And size >= 8) ' Test local variable. If isBigCat And color = "grey" Then Console.WriteLine(True) End If If isBigCat And color = "white" Then Console.WriteLine(False) End If End Sub End Module Output True
The If-statement is the most common selection statement in VB programs. But it is not our only choice. We can instead use the Select-Case statement. This compares a value against a set of constants.Select Case
For strings, we also consider the Select-Case statement. This can lead to clearer syntax. But my experiments in VB.NET programs found that no advanced Dictionary-based optimizations were applied.
So:Select Case on strings does not improve performance much over the If-statement in this language.
There are many ways to optimize an If-statement. Often program performance analysis is complex. And making your If-statements faster will help little. But these optimizations still have an effect.
1. Test common first.If-statements are evaluated in sequential order. The most common cases should be tested first.
2. Select Case.A Select-Case statement may improve performance. The Select-Case is implemented with a switch opcode.Opcodes
3. Use Dictionary.A Dictionary collection uses hash lookups to locate values. For large data sets, this is much faster.Dictionary
4. Store results.Assign the result of an expression to a variable. Then use that value, not complex If-statements, to test the condition.
If-statements are used throughout programs. They use a slightly different syntax form from many languages. We use the Not-keyword to test negatives for truth. And we end the statement block with an "End If" line.