Practice with Arithmetic Operations
You can see the result of an expression by displaying it in the text of the form. Add this code for the form load event:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.Text = 17 Mod 5
End Sub
The result, 2, will be visible as the title of the program as soon as the program is visible.
Order of Operations
An expression can have more than one arithmetic operation. If there is more than one arithmetic operation in an expression, they are executed according to the level of the operators: Parenthesis take precedence over all other operators; multiplication and division take precedence over mod, mod takes precedence over addition and subtraction.
Evaluate an expression in the following order:
- Anything inside parenthesis, including functions, is done first: working from the inner-most parenthesis out;
- Raise to a power: ^ is performed from left to right;
- Multiplication and division (*, / , \) are performed from left to right;
- Mod is performed from left to right;
- Addition and subtraction ( + and -), are performed from left to right.
Example:
3 + 5 * 2 is 13.
(Multiplication is done before addition.)
If you want to add first you must use parenthesis:
(3 + 5) * 2 is 16.
(Parenthesis are done first.)
INDEX,
Arithmetic Operations,
Modulus Operator,
Order of Operations,
Functions,
Algebra to CodeNext lesson:
Visual Basic Variables