VB.NET If Then Else statement

This article covers the use of the if then else statement in VB.NET.

The VB.NET if then else statement executes a block of code, if a specified condition holds returns True. If the condition returns False however the statement will end, or you can choose to have another block of code executed using the else statement.

VB.NET operators are used heavily in if then else statements to create the expressions that will return either True or False, especially comparison and logical operators.


Syntax

Only the If and End If keywords here are actually required. The ElseIf and Else keywords are optional and can be skipped if not required.

If (condition) Then
     #Statement to be executed
ElseIf (condition) Then       # Optional
     #Statement to be executed
Else:                         # Optional
     #Statement to be executed
End If

If Statement

A short example demonstrating it’s use, in it’s simplest form without any optional statements. Remember to close the If statement using the End If line.

Dim a as Integer = 5
Dim b as Integer = 2

If (a > b)
    Console.WriteLine("a greater than b") 
End If
"a greater than b"

Else statement

The else statement triggers if the condition(s) in the if statement were/was false. (This includes any elif statements that may be present). In simpler words, it’s a block that runs if all else has failed.

Dim b as Integer = 2

If (a > b)
    Console.WriteLine("a greater than b") 
Else:
    Console.WriteLine("b is greater than a")
End If

We haven’t specified a value for a here under the assumption that the user will be inputting a value.


Else If Statement

As mentioned before we may have multiple conditions in the if else statements. Using Else If statements we can define multiple conditions, thus multiple routes that the program flow might take.

Keep in mind however that whatever happens, only one route can be taken. Only one out of the many code blocks will be executed. This applies to all the code blocks in a Python if else statements, including if and else code blocks.

Examples for the Else If statement will be shown further into this article.


Example 1

The following examples utilizes all the possible keywords and statements in the VB.NET if then else statement.

Module Module1
    Sub Main()
        Dim x As Integer = 5

        If (x > 0) Then
            Console.WriteLine("A is positive")
        ElseIf (x < 0) Then
            Console.WriteLine("B is positive")
        Else
            Console.WriteLine("A is zero")
        End If

    End Sub
End Module

The above if else then statement has two main conditions. One that triggers if the number x is less than 0, and the second triggers if x is greater than zero. However, there is also an else that triggers it neither of the above conditions returned True. In this case, if x was equal to zero.


Using Multiple Conditions and operators

Here we’ll discuss the use of multiple conditions in if else statements. Since we need to use operators to chain together two or more conditions, we’ll be discussing the use of logical and comparision operators here too.

Example 2

The following code from the user two inputs and proceeds to use them in the if else statement below. Both conditions are chained together using the AND logical operators, which returns True only if both conditions are return True.

Module Module1
    Sub Main()
        Dim Input1 As String = Console.ReadLine()
        Dim Input2 As String = Console.ReadLine()

        If (Input1 > 0) And (Input2 > 0) Then
            Console.WriteLine("A is positive")
        Else
            Console.WriteLine("Both Numbers are not positive")
        End If
        Console.Read()


    End Sub
End Module

You can also use other operators besides AND, such as OR and XOR.


This marks the end of the VB.NET if else statement article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked below in the comment section.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments