Conditional Operator
Selects one of two expressions to calculate because of a condition.
Syntax
If condition Then true_result Else false_result
Parts
condition
- A Boolean expression
true_result
- An expression.
When
condition
is true, it becomes the result of the conditional expression. false_result
- An expression.
When
condition
is false, it becomes the result of the conditional expression.
Instructions
The conditional operator can look almost the same as the statement If…Else
.
But the operator evaluates expressions, not statements.
When condition
is true, true_result
evaluates and its value becomes the result of the operation.
Thus false_result
does not evaluate.
When condition
is false, false_result
evaluates and its value becomes the result of the operation.
Thus true_result
does not evaluate.
Usually, true_result
and false_result
must be compatible data types.
But they can be different types if, for example, the outer expression can accept more than one data type, an overloaded method.
Examples
Function FootOrFeet(dist As Real) As String
Return If dist = 1.0 Then "foot" Else "feet"
End Function