Visual Basic If-Else-If Statement

In Visual Basic, If-Else-If statement or condition is useful to define the multiple conditions and execute only the matched condition based on our requirements.

 

Generally, in Visual Basic if statement or if-else statement is useful when we have a one condition to validate and execute the required block of statements. In case, if we have a multiple conditions to validate and execute only one block of code, then If-Else-If statement is useful in our application.

Syntax of Visual Basic If-Else-If Statement

Following is the syntax of defining the If Else If statement in Visual Basic programming language.

 

If condition_1 Then

// Statements to Execute if condition_1 is True

ElseIcondition_2 Then

// Statements to Execute if condition_2 is True

ElseIcondition_2 Then

// Statements to Execute if condition_3 is True

....

....

Else

// Statements to Execute if all conditions are False

End If

If you observe the above Visual Basic If-Else-If statement syntax, we defined multiple conditions to execute required statements.

 

Here, the execution of If-Else-If statement will start from the top to bottom and as soon as the condition returns true, then the code inside of If or ElseIf block will be executed and the control will come out of the loop.

 

In case, if none of the conditions return true, then the code inside of Else block will be executed.

 

Following is the simple example of using If-Else-If statement in Visual Basic programming language.

 

Dim x As Integer = 5

If x = 10 Then

Console.WriteLine("x value equals to 10")

ElseIf x > 10 Then

Console.WriteLine("x value greater than 10")

Else

Console.WriteLine("x value less than 10")

End If

If you observe above example, the if-else-if statement will start the execution from top to bottom and will check if any condition is matching or not to execute the respective code block. In case if no condition is matching, then the else block will be executed.

Visual Basic If-Else-If Statement Flow Chart

Following is the flow chart diagram which will represent the process flow of If-Else-If statement in Visual Basic programming language.

 

Visual Basic If-Else-If Statement Flow Chart Diagram

 

If you observe the above Visual Basic If-Else-If statement flow chart, when the defined condition is true, then the statements within If condition will be executed otherwise it will move to another condition (Else-If) to check whether the condition is matching or not. In case, if no conditions are matching then the Else block will be executed.

Visual Basic If-Else-If Statement Example

Following is the example of defining the If-Else-If statement in Visual Basic programming language to execute the block of code or statements based on the Boolean expression.

 

Module Module1

    Sub Main()

        Dim x As Integer = 5

        If x = 10 Then

            Console.WriteLine("x value equals to 10")

        ElseIf x > 10 Then

            Console.WriteLine("x value greater than 10")

        Else

            Console.WriteLine("x value less than 10")

        End If

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we defined the If-Else-If conditions to execute the statements based on defined condition status.

 

When we execute above Visual Basic program, we will get the result like as shown below.

 

Visual Basic If Else If Statement Example Result

 

If you observe above result, all the defined conditions are failing due to that it executed Else block of statements and printed the required statement in the console window.

 

This is how we can use the If-Else-If statement in Visual Basic programming language to execute the block of code or statements based on our requirements.