Operators (Page 1 of 4 )
Operators are the basic data manipulation tools of any programming language. All data ultimately breaks down into single bits of 0 and 1. And the whole reason a computer exists is to manipulate those single bits of data with basic operators. This article discusses the basic operators available in Visual Basic, and how they interact with data. This article is excerpted from chapter five of
Visual Basic 2005 in a Nutshell, Third Edition, written by Tim Patrick, Steven Roman, Ph.D., Ron Petrusha and Paul Lomax (O'Reilly; ISBN: 059610152X). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Operators come in two usage types: unary and binary. Unary operators work on a single operand, while binary operators require two operands. Most operators in Visual Basic are binary operators.
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 concate nation 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
Next: Concatenation Operators >>
More Visual Basic.NET Articles
More By O'Reilly Media
|
This article is excerpted from chapter five of Visual Basic 2005 in a Nutshell, Third Edition, written by Tim Patrick, Steven Roman, Ph.D., Ron Petrusha and Paul Lomax (O'Reilly; ISBN: 059610152X). Check it out today at your favorite bookstore. Buy this book now.
|
|