Section 7.3. Relational Operators

   

7.3 Relational Operators

Relational operators are used to compare two values and then return a Boolean (i.e., true or false). The greater-than operator (>), for example, returns true if the value on the left of the operator is greater than the value on the right. Thus, 5>2 returns the value true, while 2>5 returns the value false.

The relational operators for VB.NET are shown in Table 7-1. This table assumes two variables : bigValue and smallValue, in which bigValue has been assigned the value 100 and smallValue the value 50.

Table 7-1. Relational operators (assumes bigValue = 100 and smallValue = 50)

Name

Operator

Given this statement:

The expression evaluates to:

Equals

=

bigValue = 100

bigValue = 80

True

False

Not Equals

<>

bigValue <> 100

bigValue <> 80

False

True

Greater than

>

bigValue > smallValue

True

Greater than or equals

>= or =>

bigValue >= smallValue

smallValue => bigValue

True

False

Less than

<

bigValue < smallValue

False

Less than or equals

<= or =<

smallValue <= bigValue

bigValue =< smallValue

True

False

Each of these relational operators acts as you might expect. Notice that some of the operators are composed of two characters . For example, the greater than or equal to operator is created using the greater than symbol ( > ) and the equal sign ( = ). Notice that you can place these symbols in either order ( >= or => ) to form the greater than or equal to operator.

In VB.NET, the equality operator and the assignment operator are represented by the same symbol, the equal sign ( = ). In the following code line, the symbol is used in each of these ways:

 If myX = 5 Then myX = 7 

The first use of the = symbol is as the equality operator ("if myX is equal to 5"); the second use is as the assignment operator ("set myX to the value 7"). The compiler figures out how the symbol is to be interpreted according to the context.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

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