Visual Basic Arithmetic

PreviousTable of ContentsNext
Declaring Visual Basic Variables and ConstantsVisual Basic Comparison and Logic


Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99


Two areas where computer systems excel are arithmetic and logic. Given this fact, it should come as no surprise that a lot of programming tasks also involve arithmetic and logic. The purpose of this chapter is to provide an explanation of arithmetic in Visual Basic. The next chapter will cover Visual Basic Comparison and Logic.

Understanding Expressions

Arithmetic calculations and logical evaluations are defined in Visual Basic using expressions. The calculation 1 + 1 is, for example, an expression.

Expressions consist of two key components called operators and operands. The operands define the values or variable on which the arithmetic or logical evaluation are to be made. The operator defines what action is to be performed on the operands. Often, an assignment operator is also used to define what should happen to the result of the expression. This is, perhaps, best explained using an example:

Dim intResult As Integer

intResult = 4 + 5

In the above example, the numbers '4' and '5' are both operands. The '+' is an operator which adds together the two operands. Similarly, the '=' is an operator which assigns the result of the addition to the intResult variable which is an operand.

A logical expression uses operators and operands to make a decision of some sort. For example:

Dim intResult As Integer = 10

If intResult < 20 Then
       MessageBox.Show("Result is less than 10")

In the above expression, the intResult < 20 expression will return either True or False depending on whether the value held by the intResult variable is less than 20 or not.

Expressions can be made up of multiple operands and expressions:

Dim intResult As Integer

intResult = 4 + 5 - 10 * 6

This particular example raises the issue of operator precedence which we will cover next.

Visual Basic Operator Precedence

In an earlier example we looked an expression which contained a variety of different operators. Another example is as follows:

Dim intResult As Integer

intResult = 4 + 5 * 20

The issue we have to address now is in what order these calculations will be performed. If Visual Basic were to evaluate the expression from left to right the result would be 180. If the expression is evaluated from right to left the result would be 104. So, which way does Visual Basic evaluation expressions? The answer is neither of these options. Instead, Visual Basic evaluates expressions according to operator precedence. This is essentially a predefined order in which expressions are calculated which matches the standard algebraic order. The order used by Visual Basic is as follows:

<google>ADSDAQBOX_FLOW</google>

ArtithmeticLogical
Exponentiation (^)Not
Polarity (+,-)
Negation (-)And
Mutliplication and Division (*/)Or
Integer division (\)
Modulus (mod)
Addition and Subtraction (+,-)
String Concatenation (&)

Given this order of precedence, our example will evaluate in the order of multiplication followed by addition resulting in a value of 104.

Now that we understand what expressions are and the order of operator precedence we can move on to look at operators in action.


Visual Basic Addition

Addition is performed in Visual Basic using the plus (+) operator. For example:

Dim intValue As Integer

intValue = 10 + 5 + 6

Variables may also be used as operands:

Dim intValue, intOperator1, intOperator2 As Integer

intOperator1 = 10
intOperator2 = 5

intValue = 10 + intOperator1 + intOperator2

Visual Basic Subtraction and Negation

The subtraction (-) is used to perform subtraction in Visual Basic:

Dim intValue, intOperator1, intOperator2 As Integer

intOperator1 = 10
intOperator2 = 5

intValue = 10 - intOperator1 - intOperator2

A negative number is denoted by prefixing the number with the (-) sign:

intOperator2 = -67

Visual Basic Multiplication

Visual Basic, like most other programming languages, use the (*) operator (rather than the x we all use when writing mathematical expressions) to perform multiplications:

Dim intValue, intOperator1, intOperator2 As Integer

intOperator1 = 10
intOperator2 = 5

intValue = intOperator1 * intOperator2

Visual Basic Division

Division is achieved in Visual Basic using the / operator. In division, the left hand operand is divided by the right hand operand. For example, the following example returns 5 (20 divided by 4):

Dim intValue, intOperator1, intOperator2 As Integer

intOperator1 = 20
intOperator2 = 4

intValue = intOperator1 / intOperator2

Visual Basic Exponentiation

Exponentiation involves raising a number to a particular power. For example 103 would evaluate to 1000. The carat (^) character is used to represent exponentiation in Visual Basic:

Dim intValue As Integer

intValue = 10 ^ 3

Visual Basic Modulus Arithmetic

Modulus involves the division of two numbers and obtaining the remainder of that division. Modulus calculations are performed in Visual Basic using the Mod keyword. For example:

10 Mod 2 - returns 0
10 Mod 4 - returns 2
12 Mod 5 - returns 2


Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99