Value Comparisons (Visual Basic)

Comparison operators can be used to construct expressions that compare the values of numeric variables. These expressions return a Boolean value based on whether the comparison is true or false. Examples of such an expression are as follows.

45 > 26

26 > 45

The first expression evaluates to True, because 45 is greater than 26. The second example evaluates to False, because 26 is not greater than 45.

You can also compare numeric expressions in this fashion. The expressions you compare can themselves be complex expressions, as in the following example.

x / 45 * (y +17) >= System.Math.Sqrt(z) / (p - (x * 16))

The preceding complex expression includes literals, variables, and function calls. The expressions on both sides of the comparison operator are evaluated, and the resulting values are then compared using the >= comparison operator. If the value of the expression on the left side is greater than or equal to the value of the expression on the right, the entire expression evaluates to True; otherwise, it evaluates to False.

Expressions that compare values are most commonly used in If...Then constructions, as in the following example.

If x > 50 Then
    ' Insert code to run if x is greater than 50.
    ' Insert code to run if x is less than or equal to 50.
End If

The = sign is a comparison operator as well as an assignment operator. When used as a comparison operator, it evaluates whether the value on the left is equal to the value on the right, as shown in the following example.

If x = 50 Then
    ' Insert code to continue program.
End If

You can also use a comparison expression anywhere a Boolean value is needed, such as in an If, While, Loop, or ElseIf statement, or when assigning to or passing a value to a Boolean variable. In the following example, the value returned by the comparison expression is assigned to a Boolean variable.

Dim x As Boolean
x = 50 < 30
' The preceding statement assigns False to x.

See also