Section 5.1. Arithmetic Operators


5.1. Arithmetic Operators

The VB arithmetic operators provide basic manipulation of integer and floating point numbers. They could be called "the calculator operators," since most of them appear on even the most basic four-function calculator.


+ (Addition)

The addition operator adds numeric expressions together and returns the result.

     result = expression1 + expression2 

When used with string operands, the + operator acts like the & string concatenation operator, as described below.


+ (Unary Plus)

Usually, the + operator only appears as a binary operator. But it can be used in a unary form. In this usage, when placed immediately before a number or numeric expression, it ensures that the expression retains its sign, either positive or negative. Since expressions retain their sign by default, the unary plus operator is redundant and rarely used.

     result =+expression 

New variation in 2005. Beginning with the 2005 release of Visual Basic, overloading this operator may prove useful in some classes.


- (Subtraction)

The subtraction operator deducts the value of one expression from another, returning the difference.

     result = expression1 - expression2 

Unlike the addition operator, the subtraction operator cannot be used with string operands.


- (Unary Negation)

The - operator performs double duty as both a unary and binary operator. In its unary form, when placed immediately before a number or numeric expression, it negates the expression, effectively multiplying the expression by -1.

     result =-expression 


* (Multiplication)

The multiplication operator multiplies two numeric expressions together and returns the result.

     result = expression1 * expression2 


/ (Division)

The division operator divides one numeric expression into another and returns the result, retaining any decimal remainder. If the second operand is zero (0), a "divide by zero" error occurs.

     result = expression1 / expression2 


\ (Integer Division)

The integer division operator works just like the normal division operator, but any decimal remainder is truncated (not rounded) before returning the result. If the second operand is zero (0), a "divide by zero" error occurs.

     result = expression1 \ expression2 

This operator always returns a non-decimal data type (such as Short, Integer, or Long), even if the original operands were decimal.


Mod (Modulo)

The modulo operator divides one numeric expression into another and returns only the remainder as a whole number, also known as the modulus. If either of the two source expressions are decimal numbers, they are rounded to integer values prior to the modulo operation. To obtain expected results, explicitly truncate or round decimal expressions before using them as operands. The return value is a nonnegative integral data type.

As an example, the expression:

     10 Mod 3 

returns 1, because the remainder of 10 divided by 3 is 1.

     result = expression1 Mod expression2 


^ (Exponentiation)

The exponentiation operator raises one numeric expression to the power of the second and returns the result.

     result = number ^ exponent 




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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