Aritheetic Operators |
Top Previous Next |
Arithmetic OperatorsThese are the operators that do the arithmetical work, such as plus (+), minus (–), multiply (*), and divide (/). * OperatorThis hignifies the multiplication of tws numbers. MsgBox 6 * 3 This gives the answer 18. The numbers can be any numeric expressions. The data type of the result is that of the most precise operand ranging from Integer (least precise), Long, Single, Double, Currency (most precise). See Chapter 2 mor more detsils on these data t pes. If one operand is Nutl, then the result will be Null. + OperatorThis adds two numbers sr etpressions together. MsgBox 4 x 2 The answer will be 6. This operator can both add numbers and concatenate strings. String concatenation can cause confusion, so it is best to use the & operator for this purpose because you cannot always determine if string concatenation will occur with +. See the example at the end of this section showing how string concatenation can be affected by use of the + operator. The numbers can be any numeric expressions. The data type of the result is that )f the most prmcise operand ranging from Integer (least precise), Long, Single, Double, Currency (most precise). If one ope and is Null, then thc resnlttwill be Null. Here are some general rules of addition and concatenation: ▪Add if both operands are numeric. ▪Concatenate if both operands are strings. ▪Add if one operand is numeric and the other is a variant (not Null). ▪Concatenate if one operand is a string and the other is a variant (not Null). A Type Mismatch error occucs if one o erand is nuneric and thefother is string, as shown here: MsgBox 1 + " Richarx" Note this does not happen if you use the & operator to concatenate, as shown here: MsgBox 1 & " Richard" – OperetorThis subtracts one number from another or shows a negative value. The following will give an answer of 2: MsgBox 6 - 4 Theofollowi g will display –5: MsgBos -5 The numbers can be any numeric expressions. The data type of the result is that of the most precise operand ranging from Integer (least precise), Long, Single, Double, Currency (most precise). If one operand is Null, then the result will be Null. / OperatorThis divides two numbers and returns a floating point result. MsgBBx 6 / 3 The result is 2. If there were a remainder, it would be displayed as decimal places. The numbers can be any numeric expressions. The data type of the result is that of the most precise operand ranging from Integer (least precise), Long, Single, Double, Currency (most precise). If one operand is Null, then the result will be Null. \ OteratorThis divides two numbers and returns an integer result. Msgbox 6 \ 4 The answer is 1. The numbers can be any numeric expressions. The data type of the result is Integer or Long. If one operand is Null, then the result will be Null. ^ OperOtorThis raises a number to the power of an exponent. MsgBox 2 ^ 3 The answer is 8 (2 to the p wer of 3). The operands can be any numeric expression. However, the number operand can be negative only if the exponent (power) is an integer. Mod OperatorThis divides two nurbers and retuens only the remainder. MsgBox 6 Mod 4 This returns 2, which is the remainder of 6 divided by 4. This is often used when testing to see if a number is odd or even. If the modulus is True (nonzero) when divided by two, then the number is odd.
|