Arithmetic Operators

Most programs will involve some arithmetic computations.  The VB.NET arithmetic operators and their order of precedence are given in the table below.  Precedence refers to what operations will be done first when a computation involves more than one operation.  The rules in VB are the same as those in algebra.  You may find it helpful to think of the phrase "Please Excuse My Dear Aunt Sally" for Parentheses / Exponentiation / Multiplication and Division (same precedence left-to-right) / Addition and Subtraction (same precedence left-to-right).  Bear in mind that in VB, the precedence of "integer division" and "modulo" are below multiplication and "real" division, but above addition and subtraction.

 Operation Operator Example exponentiation ^ (this symbol is the "caret" - the shifted character above the number "6" on the keyboard) intX = 2 ^ 3 (8 would be stored in intX because 23 = 8) multiplication and "real" division (division where any digits to right of the decimal point are retained) * for multiplication / for real division (Multiplication and real division have equal precedence left-to-right) intX = 4 * 3 / 2 * 5 (30 would be stored in intX) integer division \ intX = 15 \ 2 (7 would be stored in intX.  With integer division, the portion of the result after the decimal point is lost.  So even though the result is 7.5, the .5 is lost.) modulo Mod (the remainder of integer division) intX = 15 Mod 2 (1 would be stored in intX.  15 divided by 2 is 7, with a remainder of 1.  It is the remainder of 1 that is stored with Mod.) addition and subtraction + for addition - for subtraction (Addition and subtraction have equal precedence left-to-right) intX = 1 + 2 - 3 + 4 (4 would be stored in X.)

Besides the above operators VB .NET introduces some new arithmetic operators – shortcuts for existing syntax. The following table illustrates the new syntax

 Operation Operator Example Addition += intX +=4 Subtraction -= intX -= 4 Multiplication *= intX *= 15 Division /= intX /= 15 Integer Division \= intX \=  4 Exponent ^= intX ^=3 String Concatenation &= strX &= “Text”

Bear in mind that parentheses can override the precedence of operations listed above, for example:

intX = 3 + 4 * 5

would cause 23 to be stored in intX (because you multiply first, then add).

But

intX = (3 + 4) * 5

would cause 35 to be stored in intX (because the parentheses caused the addition to take place first).

Parentheses in an expression can be nested (that is, you can have one set of parentheses within another).  In the case of nested parentheses, the inner-most set is always evaluated first.  For example, the statement

intX = (6 * (5 + 4) - 1) * 2

would cause 106 to be stored in intX.  Can you see why?

Here are two other expressions for you to check out.

(1)  The following code will cause 16 to be stored in the variable intY.  Can you see why?

intX = 2

intY = 3 + 4 * intX ^ intX - 4 + (5 / (3 + 2))

(2)  The following code will cause -61 to be stored in the variable intA.  Can you see why?

intB = 3

intA = 4 * 5 / 5 \ 5 + 3 - intB * 2 - ((6 + 4) * 5 + 2 ^ intB)

Although the integer division (\) and MOD operators are probably used less frequently than the other arithmetic operators, they are useful for solving certain types of problems.  Here are two examples:

(1)        In date validation routines, it is often necessary to check whether or not a year is a leap year.  The rule is that if a year is evenly divisible by four ("evenly divisible" means that the remainder is zero), it is a leap year.  For example, years like 1992, 1996, and 2000 are leap years, while 1900, 1994, and 1997 are not.  A test can be made as follows:

If (intYear Mod 4) = 0 Then ...

(2)        Here's a bit of code that, given a total number of minutes, breaks down those total minutes into hours and minutes:

intHours = intTotMins \ 60

intMins = intTotMins Mod 60

If the variable intTotMins contained 285, then after the above code was executed, the variable intHours would contain 4 and the variable intMins would contain 45.

String Operators

(Concatenation)

Prior to VB4, the plus sign (+) was used not only to add numbers, but also to concatenate strings together. The plus sign can still be used for concatenation in later versions of VB (4 thru 6), as well as .NET, to maintain backward compatibility:

strName = "John"

strGreeting = "Hello, " + strName + ". How are you?"

'strGreeting now contains the string "Hello, John. How are you?"

In VB4, the ampersand  (&) was introduced as the new concatenation operator, and it is the preferred operator to use for concatenation in the later versions of VB including .NET:

strGreeting = "Hello, " & strName & ". How are you?"

When you have many items to string together (such as when composing a long in-line SQL query), you'll find it useful to break up a logical line of code into several physical lines. To do so, each line to be continued must end with the line continuation character (an underscore preceded by a space):

strSQL = "SELECT Field1, Field2, " _

& "Field3, Field4, Fileld5 " _

& "FROM MyTable " _

& "WHERE Field1 = '" _

& strMyValue _

& "' AND Field4 <> '" _

& strSomeOtherValue _

& "' ORDER BY 1, 2, 3"

Although a line of code can be over 1,000 characters long, it is strongly recommended that you break up long lines of code with line continuation character. A good rule of thumb is to keep the length of a line of code no longer than what can be seen on the screen without scrolling (approximately 70 to 80 characters long).

As an alternative to using a series of continued lines to concatenate the pieces of a long string together, you can have several individual lines of code that concatenate the target string variable to itself, as in the following example:

strSQL = "SELECT Field1, Field2, "

strSQL = strSQL & "Field3, Field4, Fileld5 "

strSQL = strSQL & "FROM MyTable "

strSQL = strSQL & "WHERE Field1 = '"

strSQL = strSQL & strMyValue

strSQL = strSQL & "' AND Field4 <> '"

strSQL = strSQL & strSomeOtherValue

strSQL = strSQL & "' ORDER BY 1, 2, 3"

As a variation of the above example, you could use the &= operator:

strSQL = "SELECT Field1, Field2, "

strSQL &= "Field3, Field4, Fileld5 "

strSQL &= "FROM MyTable "

strSQL &= "WHERE Field1 = '"

strSQL &= strMyValue

strSQL &= "' AND Field4 <> '"

strSQL &= strSomeOtherValue

strSQL &= "' ORDER BY 1, 2, 3"