16.7. Operators


An operator is a symbol (e.g., =, >, +) that causes VB.NET to take an action. That action might be assignment (=), addition of two values (+), or comparison of two values (>).

The simplest is the assignment operator:

 myVariable = 15; 

The single equal sign (=) is used to assign a value (15) to a variable (myVariable). If statements often use comparison operators :

 if ( valueOne > valueTwo ) 

This If statement compares valueOne with valueTwo, and if the former is larger than the latter, the test evaluates TRue, and the If statement executes.

16.7.1. Arithmetic Operators

VB.NET uses seven arithmetic operators , six for standard calculations (+, -, *, ^, /, and \) and a seventh to return the remainder when dividing integers.

VB.NET offers two division operators: / and \. The right-facing division operator (/) returns a floating-point answer. If you divide 12/5, the answer is 2.4, as you would expect. This answer is returned as a Double. If you assign the returned value to an Integer, the decimal part is lopped off, and the result will be 2. If Option Strict is On (as it should be), you cannot assign the result to an Integer without explicitly casting, because you would lose the decimal portion of the answer.

The left-facing division operator (\) returns an Integer value. If you divide 12\5, the result is returned with a truncated integer: 2. No cast is needed (even with Option Strict On) because you've explicitly asked for the integer value.

16.7.1.1. The modulus operator (Mod) modulus operator (Mod)

To find the remainder in integer division, use the modulus operator (Mod). For example, the statement 17 Mod 4 returns 1 (the remainder after integer division).

The modulus operator is more useful than you might imagine. When you perform modulus n on a number that is a multiple of n, the result is zero. Thus, 80 Mod 10 = 0 because 80 is an even multiple of 10. This allows you to set up loops in which you take an action every nth time through the loop, by testing a counter to see if Mod n is equal to zero.

16.7.1.2. The exponentiation (^) operator

The final arithmetic operator is the exponentiation operator , which raises a number to the power of the exponent:

 2^8 = 1024 ' 2 to the 8th power is 1,024. 

16.7.2. Relational Operators

Relational operators are used to compare two values, and then return a Boolean (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 16-5. This table assumes two variables: bigValue and smallValue, in which bigValue has been assigned the value 100 and smallValue the value 50.

Table 16-5. 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


Notice that some operators are composed of two characters. For example, greater than or equal to is created with the greater-than symbol (>) and the equal sign (=). You may place these symbols in either order (>= or =>)

The VB.NET equality operator (=) and assignment operator (=) use the same symbol.

When you write:

 If myX = 5 Then myX = 7 

the first use of the = symbol is the equality operator (if myX is equal to 5), the second use is the assignment operator (set myX to the value 7). The compiler figures out which is meant by context. (Eat your heart out, C# programmers!)

16.7.3. Use of Logical Operators with Conditionals

If statements test whether a condition is true. Often you will want to test if two conditions are both TRue, only one is TRue, or none is TRue. VB.NET provides a set of logical operators for this, as shown in Table 16-6. This table assumes two variables, x and y, in which x has the value 5 and y the value 7.

Table 16-6. Logical operators given x = 5 and y = 7

Operator

Given this statement

The expression evaluates to

Logic

And

x = 3 And y = 7

False

Both must be TRue, to evaluate TRue.

AndAlso

X = 3 AndAlso y = 7

False

Short circuits the test, y is never tested. If the left side fails, the right is not tested.

Or

x = 3 Or y = 7

true

Either or both must be TRue to evaluate TRue.

OrElse

X = 5 or y = 9

TRue

Short circuited, since x is equal to 5, no need to evaluate y.

Xor

X = 5 Xor y = 7

False

True only if one (and only one) statement is true.

Not

Not x = 3

TRue

Expression must be False for test to evaluate TRue.


The And operator tests whether two statements are both TRue. The first line in Table 16-6 includes an example that illustrates the use of the And operator:

 x = 3 And y = 7 

The entire expression evaluates False because one side (x = 3) is False.

With the Or operator, either or both sides must be true; the expression is False only if both sides are False. So, in the example in Table 16-6:

 x = 3 Or y = 7 

the entire expression evaluates to true because one side (y = 7) is TRue.

The Xor logical operator is used to test if one (and only one) of the two statements is correct. Thus:

 x = 5 Xor y = 7 

evaluates to False, because both statements are TRue. The Xor statement is False if both statements are TRue, or if both statements are False; it is true only if one and only one statement is true.

With a Not operator, the statement is true if the expression is False, and vice versa. So, in the accompanying example:

 Not x = 3 

the entire expression is true because the tested expression (x = 3) is False. (The logic is: "it is true that it is not true that x is equal to 3.")

16.7.4. Operator Precedence

The compiler must know the order in which to evaluate a series of operators. For example, if I write:

 myVariable = 5 + 7 * 3 

there are three operators for the compiler to evaluate (=, +, and *). It could, for example, operate left to right, which would assign the value 5 to myVariable, then add 7 to the 5 (12) and multiply by 3 (36). Since we're evaluating left to right, the assignment has been done, so the value 36 is thrown away. This is clearly not what is intended.

The rules of precedence tell the compiler which operators to evaluate first. As is the case in algebra, multiplication has higher precedence than addition, so 5 + 7 * 3 is equal to 26 rather than 36. Both addition and multiplication have higher precedence than assignment, so the compiler will do the math, and then assign the result (26) to myVariable only after the math is completed.

In VB.NET, parentheses are used to change the order of precedence much as they are in algebra. Thus, you can change the result by writing:

 myVariable = (5+7) * 3 

Grouping the elements of the assignment in this way causes the compiler to add 5 + 7, multiply the result by 3, and then assign that value (36) to myVariable.

Within a single line of code, operators are evaluated in the following order:

  • Parentheses

  • Arithmetic

  • Concatenation

  • Comparison

  • Logical

  • Assignment

Comparison operators are evaluated left to right. Arithmetic operators are evaluated in this order:

  • Exponentiation (^)

  • Division and multiplication (/, *)

  • Integer division (\)

  • Modulo operator (Mod)

  • Addition and subtraction (+,-)

The logical operators are evaluated in this order:

  • Not

  • And

  • Or

  • Xor



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

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