# Comparisons: Using Expressions to Compare Values

Visual Studio 2008

In this lesson, you will learn how to use comparison operators to create expressions that compare values.

The last lesson showed how to use arithmetic operators to create numeric expressions and return numeric values. Another kind of operator, the comparison operators, can be used to compare numeric values and return Boolean (True or False) values.

Comparison operators are most frequently used to compare values and make decisions based on that comparison. Making decisions in your program will be covered in-depth in Making a Program Choose Between Two Possibilities: The If...Then Statement.

The following table summarizes the comparison operators:

Operator

Description

Examples

= (equals)

Returns True if the number on the left-hand side is equal to the number on the right-hand side.

5 = 4 (false)

4 = 5 (false)

4 = 4 (true)

<> (not equal to)

Returns True if the number on the left is not equal to the number on the right.

5 <> 4 (true)

4 <> 5 (true)

4 <> 4 (false)

> (greater than)

Returns True if the number on the left is greater than the number on the right.

5 > 4 (true)

4 > 5 (false)

4 > 4 (false)

< (less than)

Returns True if the number on the left is less than the number on the right.

5 < 4 (false)

4 < 5 (true)

4 < 4 (false)

>= (greater than or equal to)

Returns True if the number on the left is greater than or equal to the number on the right.

5 >= 4 (true)

4 >= 5 (false)

4 >= 4 (true)

<= (less than or equal to)

Returns True if the number on the left is less than or equal to the number on the right.

5 <= 4 (false)

4 <= 5 (true)

4 <= 4 (true)

## Try It!

### To compare expressions

1. On the File menu, click New Project.

2. In the New Project dialog box, in the Templates pane, click Windows Application.

3. In the Name box, type Comparison and then click OK.

A new Windows Forms project opens.

4. From the Toolbox, drag two Textbox controls onto the form.

5. From the Toolbox, drag a Button control onto the form.

6. Double-click the Button to open the Code Editor.

7. In the Button1_Click event handler, type the following code:

```Dim A As Double = CDbl(Textbox1.Text)
Dim B As Double = CDbl(Textbox2.Text)
MsgBox(A > B)
MsgBox(A < B)
MsgBox(A = B)
```

The first two lines declare the variables A and B, which will hold the numeric values used in this program; they use the CDbl statement to convert the text from Textbox1 and Textbox2 into numeric values. Finally, the last three lines create expressions to compare the two variables using three basic comparison operators, and display the results of those expressions in three message boxes.

8. Press F5 to run the application.

9. Type a number in each of the text boxes and click Button1.

The first message box will display True if A (the number that you entered in the first text box) is greater than B (the number that you entered in the second text box); otherwise it will display False. The second message box will display True if A is less than B, and the third message box will display True if both numbers are the same.

Try typing different numbers into the text boxes to see how the results change.

## Next Steps

In this lesson, you learned how to use comparison operators to compare numeric values. In the next lesson, you will learn how to create and call a procedure—code that performs an action.