COMPARISON OPERATORS


Up to this point in the book, most of the conditional tests that you have seen involved comparing two values or expressions to see if they were equal. Although this is certainly a powerful type of test, there are times when you need to test the relationship between values and expressions in different ways. For example, you may want to know if one value is less than, greater than, or equal to another value. To accomplish these types of comparisons, you can use any of the comparison operators listed in Table 6.1.

Table 6.1: Visual Basic Comparison Operatiors

Operator

Description

=

Equal

<>

Not equal

<

Less than

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

To get a better feel for how to use these operators, take a look at the following example.

 Dim intSecretNumber As Integer = 6 Dim intPlayerGuess As Integer intPlayerGuess = InputBox("Type a number between 1 and 10?") If intPlayerGuess < intSecretNumber Then      MessageBox.Show("Your guess is too low.") End If If intPlayerGuess = intSecretNumber Then      MessageBox.Show("Your guess is correct.") End If If intPlayerGuess > intSecretNumber Then      MessageBox.Show("Your guess is too high.") End If 

As you can see, three IfThen statements have been set up. The first If...Then statement uses the less than operator (<) to see if the value entered by the player is less than intSecretNumber (which is 6). Likewise, the second IfThen statement checks to see if the player's guess is equal to intSecretNumber. Finally, the third IfThen statement uses the greater than operator (>) to see if the number supplied by the user is greater than intSecretNumber.




Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner
ISBN: 1592008143
EAN: 2147483647
Year: 2006
Pages: 126

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net