Difference between revisions of "Visual Basic Arithmetic"
(New page: 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 purpo...) |
(→Understanding Expressions) |
||
Line 23: | Line 23: | ||
MessageBox.Show("Result is less than 10") | MessageBox.Show("Result is less than 10") | ||
</pre> | </pre> | ||
+ | |||
+ | In the above expression, the ''intResult < 20'' expression will retunr 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: | ||
+ | |||
+ | <pre> | ||
+ | Dim intResult As Integer | ||
+ | |||
+ | intResult = 4 + 5 - 10 * 6 | ||
+ | </pre> | ||
+ | |||
+ | 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. Anotehr example is as follows: | ||
+ | |||
+ | <pre> | ||
+ | Dim intResult As Integer | ||
+ | |||
+ | intResult = 4 + 5 * 20 / 6 | ||
+ | </pre> | ||
+ | |||
+ | 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 -3.33333. If the expression is evaluated from right to left the result would be 5.5. So, which way does Visual Basic evaluation expressions? The answer is neither of these options. Instead, Visual Basic evaluates expressions according to operator precendece. 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: | ||
+ | |||
+ | <table> | ||
+ | <TH>Artithmetic<th>Logical</th> | ||
+ | <tr> | ||
+ | <td>Exponentiation (^)<td>Not</td> | ||
+ | <tr> | ||
+ | <td>Polarity (+,-)<td></td> | ||
+ | <tr> | ||
+ | <td>Negation (-)?<td>And</td> | ||
+ | <tr> | ||
+ | <td>Mutliplication and Division (*/)<td>Or</td> | ||
+ | <tr> | ||
+ | <td>Integer division (\) | ||
+ | <tr> | ||
+ | <td>Modulus (mod)<td></td> | ||
+ | <tr> | ||
+ | <td>Addition and Subtraction (+,-)<td></td> | ||
+ | <tr> | ||
+ | <td>String Concatenation (&)<td></td> | ||
+ | </table> |
Revision as of 14:02, 7 August 2007
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 cover both these topic areas in the context of Visual Basic programming.
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 retunr 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. Anotehr example is as follows:
Dim intResult As Integer intResult = 4 + 5 * 20 / 6
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 -3.33333. If the expression is evaluated from right to left the result would be 5.5. So, which way does Visual Basic evaluation expressions? The answer is neither of these options. Instead, Visual Basic evaluates expressions according to operator precendece. 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:
Artithmetic | Logical |
---|---|
Exponentiation (^) | Not |
Polarity (+,-) | |
Negation (-)? | And |
Mutliplication and Division (*/) | Or |
Integer division (\) | |
Modulus (mod) | |
Addition and Subtraction (+,-) | |
String Concatenation (&) |