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.
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 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 a condition.
In Visual Basic, the Ternary Operator will work as follow.
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 a Ternary operator in Visual Basic programming language.
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 the Ternary Operator to evaluate an 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.
This is how we can use the Ternary Operator as a substitute for if…else statement in Visual Basic programming language.
In Visual Basic, we can create a 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 a single line of the 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.
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.
This is how we can implement a nested ternary operator in Visual Basic programming language to replace if…else if statements based on our requirements.