Visual Basic Ternary Operator

In Visual Basic, Ternary Operator is a decision-making operator and it is an alternative for the if…else statement in Visual Basic programming language.

 

By using Ternary Operator, we can replace the multiple lines of if…else statement code into a single line in Visual Basic. The Ternary operator we will help you to execute the statements based on the defined conditions using comma (,) separated operator.

Syntax of Visual Basic Ternary Operator

In Visual Basic, the Ternary Operator will always work with 3 operands. Following is the syntax of defining the Ternary Operator in Visual Basic programming language.

 

If((condition_expression), first_expression, second_expression) 

If you observe the above Ternary Operator syntax, the conditional operator will return only one value from the defined expressions i.e. either first_expression or second_expression based on the value of condition.

 

In Visual Basic, the Ternary Operator will work like as follow.

 

  • In Ternary Operator, the condition expression must be evaluated to either true or false. If condition is true, the first_expression result returned by the ternary operator. 
  • In case, if condition is false, then the second_expression result returned by the operator.

As said earlier, the Ternary Operator is an alternative of if…else statement in Visual Basic programming language. For example, we can replace the following if…else statement with Ternary Operator like as shown following.

 

Dim x As Integer = 5, y As Integer = 20

Dim result As String

 

' if...else statement

If x > y Then

     result = "x greater than y"

Else

     result = "x less than y"

End If

 

' Ternary Operator statement

 

result = If((x > y), "x greater than y", "x less than y")

If you observe the above example, we simplified the if…else condition by replacing the multiple lines of if…else statement code with Ternary Operator in Visual Basic programming language.

 

Now, we will see the complete example of Ternary operator in Visual Basic programming language.

Visual Basic Ternary Operator Example

Following is the example of using Ternary Operator in Visual Basic programming language.

 

Module Module1

    Sub Main()

        Dim x As Integer = 5, y As Integer = 20

        Dim result As String

        result = If((x > y), "x value greater than y", "x value less than y")

        Console.WriteLine(result)

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

        Console.ReadLine()

    End Sub

 

End Module

If you observe the above example, we used Ternary Operator to evaluate the expression (x > y) and to show the result based on our requirements.

 

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

 

Visual Basic Ternary Operator Example Result

 

This is how we can use the Ternary Operator as a substitute of if…else statement in Visual Basic programming language.

Visual Basic Nested Ternary Operator

In Visual Basic, we can create Nested Ternary Operator by including the multiple conditional expressions as a second or third part of expressions in ternary operator and these nested ternary operators will help us to replace the if…else if statements in Visual Basic programming language.

 

Following is the example of replacing the if…else if statement with the nested ternary operator in Visual Basic programming language.

 

Dim x As Integer = 20, y As Integer = 20

Dim result As String

 

' If...else If Statement

If x > y Then

    result = "x value greater than y"

ElseIf x < y Then

    result = "x value less than y"

Else

    result = "x value equals to y"

End If

 

' Nested Ternary Operator

 

result = If((x > y), "x value greater than y", If((x < y), "x value less than y", "x value equals to y"))

If you observe the above code, we are able to replace the multiple lines of if…else if code with single line of nested ternary operator based on our requirements.

 

In Visual Basic, the conditional operator is a right associative so the expression a , b , c , d , e; evaluated as a , b , (c , d , e), not as (a , b , c) , d , e.

Visual Basic Nested Ternary Operator Example

Following is the example of defining the nested ternary operator in Visual Basic programming language.

 

Module Module1

    Sub Main()

        Dim x As Integer = 20, y As Integer = 20

        Dim result As String

        ' Nested Ternary Operator

        result = If((x > y), "x value greater than y", If((x < y), "x value less than y", "x value equals to y"))

        Console.WriteLine(result)

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

        Console.ReadLine()

    End Sub

 

End Module

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

 

Visual Basic Nested Ternary Operator Example Result

 

This is how we can implement nested ternary operator in Visual Basic programming language to replace if…else if statements based on our requirements. 

Topics Covered