7.2 Mathematical OperatorsVB.NET uses seven mathematical operators: five for standard calculations ( + , - , * , / , and \ ), a sixth to return the remainder when dividing integers ( Mod ), and a seventh for exponential operations ( ^ ). The following sections consider the use of these operators. 7.2.1 Simple Arithmetical Operators (+, -, *, /, \)VB.NET offers five operators for simple arithmetic: the addition ( + ), subtraction ( - ), and multiplication ( * ) operators work as you might expect. Adding two numbers returns their sum, subtracting returns their difference, and multiplying returns their product. VB.NET offers two division operators: / and \ . The forward slash or right- facing division operator ( / ) returns a floating-point answer. In other words, this operator allows for a fractional answer; there is no remainder. Thus, if you use this operator to divide 12 by 5 ( 12/5 ), the answer is 2.4. This answer is returned as a Double. Note that if you assign the returned value to an integer variable, the decimal part is lopped off, and the result will be 2. If Option Strict is turned On (as it should be), you cannot assign the result to an integer without explicitly casting because you would lose the decimal portion of the answer. The backslash or left-facing division operator ( \ ) performs integer division; that is, it returns an integer value and discards any remainder. Thus, if you use the integer division operator to divide 12 by 5 ( 12\5 ), the return value is truncated to the integer 2, with VB.NET discarding the remainder of 2. However, no cast is needed (even with Option Strict On) because you've explicitly asked for the integer value. Example 7-1 illustrates integer and fractional division. Example 7-1. Arithmetic operatorsOption Strict Off ' must be off to allow implicit casting of quotient to an integer Imports System Module Module1 Sub Main( ) Dim twelve As Integer = 12 Dim five As Integer = 5 Dim intAnswer As Integer Dim doubleAnswer As Double Console.WriteLine("{0} + {1} = {2}", _ twelve, five, twelve + five) Console.WriteLine("{0} - {1} = {2}", _ twelve, five, twelve - five) Console.WriteLine("{0} * {1} = {2}", _ twelve, five, twelve * five) ' integer division intAnswer = twelve \ five doubleAnswer = twelve \ five Console.WriteLine("{0} \ {1} = [integer] {2} [double] {3}", _ twelve, five, intAnswer, doubleAnswer) ' division. Assign result to both an integer and a double ' note, option strict must be off! intAnswer = twelve / five doubleAnswer = twelve / five Console.WriteLine("{0} / {1} = [integer] {2} [double] {3}", _ twelve, five, intAnswer, doubleAnswer) End Sub ' End of the Main( ) method definition End Module Output: 12 + 5 = 17 12 - 5 = 7 12 * 5 = 60 12 \ 5 = [integer] 2 [double] 2 12 / 5 = [integer] 2 [double] 2.4 In Example 7-1, you first declare two variables named twelve and five, which are initialized to the contain the numeric values 12 and 5, respectively: Dim twelve As Integer = 12 Dim five As Integer = 5 You then pass the sum, difference, and product of seven and five to the Console.WriteLine( ) method: Console.WriteLine("{0} + {1} = {2}", _ twelve, five, twelve + five) Console.WriteLine("{0} - {1} = {2}", _ twelve, five, twelve - five) Console.WriteLine("{0} * {1} = {2}", _ twelve, five, twelve * five) The results are just as you would expect: 12 + 5 = 17 12 - 5 = 7 12 * 5 = 60 VB.NET allows for two types of division, standard (/) and integer (\), which produce floating-point and integer results, respectively. In addition, the type of the variable to which you assign the answer also affects the value that is ultimately saved. You cannot assign a floating-point answer to a variable of type Integer. So, even if you perform standard division and receive a fractional answer, if you assign that answer to an Integer variable, the result will be truncated ”just as if you used integer division (\) to begin with! This is a bit confusing. Let's consider some examples. First, you'll create two local variables, intAnswer and doubleAnswer, to hold two quotients: Dim intAnswer As Integer Dim doubleAnswer As Double As the names imply, and the declarations confirm, intAnswer is a variable of type Integer (a whole number type) and doubleAnswer is a variable of type Double (a rational number type). The type of the variable affects whether it can hold a fractional answer ”a Double can, an Integer can't. Example 7-1 includes the following equations for integer division: intAnswer = twelve \ five doubleAnswer = twelve \ five The result returned by integer division, using the ( \ ) operator, is always an integer. Thus, it does not matter whether you assign the result of integer division to a variable of type Integer or to a variable of type Double. This is reflected in the output: 12 \ 5 = [integer] 2 [double] 2 Example 7-1 then uses the standard division operator ( / ), which allows for fractional answers: intAnswer = twelve / five doubleAnswer = twelve / five The standard division operator returns a floating-point answer, which can be accommodated by a variable of type Double (as in your variable doubleAnswer). But assigning the result to an Integer variable (like intAnswer) implicitly casts the result to an Integer, which results in the fractional portion being discarded, as in the following output: 12 / 5 = [integer] 2 [double] 2.4
7.2.2 The modulus Operator (Mod) to Return RemaindersTo find the remainder in integer division, use the modulus operator (Mod). For example, the statement 17 Mod 4 returns 1 (the remainder after integer division). The modulus operator turns out to be more useful than you might at first imagine. When you perform modulus n on a number that is a multiple of n , the result is zero. Thus 80 Mod 10 = 0 because 80 is an even multiple of 10. This fact allows you to set up loops in which you take an action every n th time through the loop, by testing a counter to see if Mod n is equal to zero, as illustrated in Example 7-2. Example 7-2. Using the modulus operator (Mod)Option Strict On Imports System Module Module1 Sub Main( ) Dim counter As Integer ' count from 1 to 100 For counter = 1 To 100 ' display the value Console.Write("{0} ", counter) ' every tenth value, display a tab and the value If counter Mod 10 = 0 Then Console.WriteLine(vbTab & counter) End If Next counter End Sub ' End of Main( ) method definition End Module Output: 1 2 3 4 5 6 7 8 9 10 10 11 12 13 14 15 16 17 18 19 20 20 21 22 23 24 25 26 27 28 29 30 30 31 32 33 34 35 36 37 38 39 40 40 41 42 43 44 45 46 47 48 49 50 50 51 52 53 54 55 56 57 58 59 60 60 61 62 63 64 65 66 67 68 69 70 70 71 72 73 74 75 76 77 78 79 80 80 81 82 83 84 85 86 87 88 89 90 90 91 92 93 94 95 96 97 98 99 100 100 In Example 7-2, the value of the counter variable is incremented by one each time through the For loop. Within the loop, the value of counter is compared with the result of modulus 10 ( counter Mod 10 ). When this evaluates to zero, meaning the value of counter is evenly divisible by 10, the value is printed in the righthand column.
7.2.3 The Exponentiation Operator (^)The final arithmetic operator is the exponentiation operator ( ^ ), which raises a number to the power of the exponent. Example 7-3 raises the number 5 to a power of 4. Example 7-3. The exponentiation operatorOption Strict On Imports System Module Module1 Sub Main( ) Dim value As Integer = 5 Dim power As Integer = 4 Console.WriteLine("{0} to the {1}th power is {2}", _ value, power, value ^ power) End Sub ' End of the Main( ) method definition End Module Output: 5 to the 4th power is 625 |