Visual Basic If Else Statement

In Visual Basic, If Else statement or condition is having an optional Else statements and the Else statements will be executed whenever the If condition fails to execute.

 

Generally, in Visual Basic If Else statement, whenever the boolean expression returns true, then the If statements will be executed otherwise the Else block of statements will be executed.

Syntax of Visual Basic If Else Statement

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

 

If boolean_expression Then

// Statements to Execute if boolean expression is True

Else 

// Statements to Execute if boolean expression is False

End If

If you observe the above If Else statement syntax, the statements inside of If condition will be executed only when the “bool_expression” returns true otherwise the statements inside of Else condition will be executed.

 

Following is the sample example of using If Else statement in visual basic programming language.

 

Dim x As Integer = 20

 

If x >= 10 Then

    Console.WriteLine("x is Greater than or equals 10")

Else

    Console.WriteLine("x is less than or equals to 10")

End If

If you observe the above example, whenever the defined condition (x >= 10) returns true, then If condition will be executed otherwise the Else block of code will be executed.

Visual Basic If Else Statement Flow Chart

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

 

Visual Basic If Else Statement Flow Chart Diagram

If you observe the above Visual Basic If Else statement flow chart diagram, when the defined condition is true, then the statements within the If condition will be executed otherwise the statements within the Else block will be executed.

Visual Basic If Else Statement Example

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

 

Module Module1

    Sub Main()

        Dim x As Integer = 20

        If x >= 10 Then

            Console.WriteLine("x is Greater than or Equals to 10")

        Else

            Console.WriteLine("x is 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 an If Else conditions to execute the statements based on the defined condition status i.e. either true or false.

 

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

 

C# If Else Statement Example Result

 

If you observe the above result, the Boolean expression returns true that’s the reason only If block of statements has been executed and printed the statements in console window.

 

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

Topics Covered