Hey guys, i came upon a particularly interesting part in my VB Reference and i'd thought i'd share it with you guys. This may be revision for some of you guys, but i'd like to help those that do not know. If you could, NextGen please place a reference of this in the Tutorials thread.
Arithmetic Expressions
Before you write a program to calculate numbers, you need to be familiar with Arithmetic expressions. These are very similar to the arithmetic operations that we do by hand or calculator. When you use a calculator, you punch specific keys such as a minus of plus sign to deduct or add numbers. Computers need to know these operators as well.
Arithmetic Operators
Like your calculator, computers need to know what type of arithmetic operations you would like to perform. You communicate your needs through symbols that a computer can understand. These symbols are called arithmetic operators. I will draw these operators in a table below. (Well i'll try )
XD i'll draw a table in paint.NET God you guys owe me took me so long to make it good . Take a look:
Precedence of Operations
We need to become familiar with the term precedence, or order of operations, for these operators. Precedence is the hierarchy or the priority that Visual Basic gives to these operators. Simply put, Visual Basic uses a guideline to perform arithmetic operations. This becomes important when more than one kind of arithmetic operator appears within one expression. Under the normal order of operation, these operators are treated as follows.
Exponentiation
Multiplication or division, depending on the order of their appearance from left to right
Integer division
Mod
Addition or subtraction, depending on the order of their appearance from left to right.
This list tells visual basic to perform the exponentiation calculation first. An example will help us understand this concept better.
3+2*5
The result is 13, not 25. Why? If we follow the rules given above, the first operation will be 2*5, which is 10. The second operation will be 3+10, which becomes 13. Lets have a look at some more examples.
2*10^2+2
The result is 202. Here is how: the first operation is 10^2 because exponentiation has the highest priority of the operators in this arithmetic operation. The result of the first operation, which is 10^2, is 100. The second operation is 2*100 because multiplication has a higher priority than addition. The result of the second operation is 200. Now the third operation will take place, which is 200+2, because the addition has the lowest priority in this expression. Therefore, the result is 202. Occasionally we want to force a specific order of operation. Considering out first example, 3+2*5, let us say we really want 3+2 to be calculated first and then be multiplied by 5. Then we need to change the order of operation. In this case, the parentheses are used to give higher priority to an operation. And yeah if you don't know what parentheses are, they're brackets.
(3+2)*5
The result is 25. Therefore the operation in parentheses always has the highest priority in any arithmetic operation. Here is an example of a mod operator.
10 mod 8
The result is 2. Mod divides 10 by 8 and reports the remainder of the calculation.