Page 111 - Open Soource Technologies 304.indd
P. 111
Unit 5: VB String and Operators
5.2.2 Multiplication Operator
We simply use the multiplication operator—the asterisk (*)—to multiply two or more numbers.
The syntax of a multiplication statement is almost identical to the ones used for addition and
subtraction, as follows:
Result = NumberOne * NumberTwo * NumberThree
As before, Result is the name of a variable used to contain the product of the numbers being
multiplied, and NumberOne, NumberTwo, and NumberThree are numeric variables. Again, you
also can use literal numbers or a return value from a function.
5.2.3 Division Operator
Division in Visual Basic is a little more complicated than multiplication. Last article showed three
division operators. The most common one is ‘ / ‘. This type is known as floating-point division
(the normal type of division). This type of division returns a number with its decimal portion,
if one is present.
Visual Basic supports two other ways to divide numbers: integer division and modulus (or
remainder) division.
Integer division divides one number into another, and then returns only the integer portion of
the result. The operator for integer division is the backward slash (\):
Result = NumberOne \ NumberTwo
Result = 2.3 \ 2 ‘The value of Result is 1
Modulus or remainder division divides one number into another and returns what’s left over
after you obtain the largest integer quotient possible. The modulus operator is the word Mod,
as follows:
Result = NumberOne Mod NumberTwo
Result = 11 Mod 3 ‘result in 2
‘(11/3 = 3 with a remainder of 2)
5.2.4 Exponents
Exponents also are known as powers of a number. For example, 2 raised to the fourth power
is equivalent to 2 × 2 × 2 × 2, or 16. Exponents are used quite a lot in computer operations, in
which many things are represented as powers of two. Exponents also are used extensively in
scientific and engineering work, where many things are represented as powers of 10 or as natural
logarithms. Simpler exponents are used in statistics, in which many calculations depend on the
squares and the square roots of numbers.
To raise a number to a power, you use the exponential operator, a caret (^). Exponents greater
than one indicate a number raised to a power. Fractional exponents indicate a root, and negative
exponents indicate a fraction. The following is the syntax for using the exponential operator:
Result = NumberOne ^ Exponent
LOVELY PROFESSIONAL UNIVERSITY 105