Mathematical OperatorsAfter having studied addition and subtraction operators, we will take a look at the multiplication, division and exponent operators in this article Multiplication OperatorWe 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. Division OperatorDivision 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) ExponentsExponents also are known as powers of a number. For example, 2 raised to the fourth power is equivalent to 2x2x2x2, 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 Example: 2 ^ 4 `result is 16 In the next article, we will try to see the hierarchy of mathematical operations.
The copyright of the article Mathematical Operators in Learning Visual Basic is owned by Swapna Kamat. Permission to republish Mathematical Operators in print or online must be granted by the author in writing.
Go To Page: 1 Articles in this Topic Discussions in this Topic |