Array Class Collections File String Console Data Directive Enum Exception If Interface Lambda Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows
The If-statement affects control flow. Its condition is evaluated by the execution engine. If the result is true, the inner statements are next processed. In VB.NET, we use If-Then, along with ElseIf, Else and End.
Based on: .NET 4.5
In VB.NET, the 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:In VB.NET, 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.Visual Studio
Program that uses If, ElseIf and Else: VB.NET 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. In VB.NET 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 in VB.NET. We must apply the Not-operator and then an equals expression.
Program that uses If Not: VB.NET 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
An If-statement does not always directly test values. Sometimes, it can call another Function and test the value returned by that Function. In this example, the If-statement calls the IsValid Function.
Then:The IsValid Function is evaluated. It returns True if the Integer argument is within a certain range.
So:If the Integer is within the specified range (10 to 100 inclusive) the inner block of the If-statement is reached.
Program that uses Function, If-statement: VB.NET 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
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
Also:The VB.NET language provides support for Select-Case statements on String variables.Select Case String
There are many ways to optimize an If-statement in a VB.NET program. These statements are evaluated in sequential order. This means the most common cases should be tested first. This reduces the total number of tests in program execution.
In some programs, using a Select-Case statement to replace an If-statement will improve performance. The Select-Case is implemented with a switch opcode. The If-statement is implemented with branch opcodes.Opcodes
Caution:A Select-Case is not always faster than an If-statement. Performance is highly dependent on the input being processed.
If-statements are used throughout VB.NET 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.