A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/Arithmetic operators





Visual Basic .NET provides a basic set of operators to calculate simple arithmetic.

+  Addition -  Subtraction *  Multiplication /  Division \  Integer division Mod Remainder Division ^  Exponentiation &  String concatenation 7 + 2    produces 9 7 - 2    produces 5 7 * 2    produces 14 7 / 2    produces 3.5 7 \ 2    produces 3 7 Mod 2  produces 1 7 ^ 2    produces 49 "7" & "7" produces "77"

Let's look at a short example of arithmetic operations before we jump into the operators themselves.

In this example we will also be using some basic variables. The Dim operator creates the variable. First, we set the total  to 3142.51.

The * operator calculates multiplication, so line 4 is equivalent to multiplying 0.3 and Sales together. Sales is 3142.51, so our result should be the product of 0.3 and 3142.51, and stored in.

Why the funny symbols?
With the exception of addition and subtraction, the symbols used are different to the ones used in real life. This is simply because the other symbols are not available on a standard keyboard (try and find ÷ ≠ m² on yours!) or the symbol is in the alphabet and can be used for naming a variable (x).

Addition
This adds two numbers together, and is denoted by the "+" symbol. If strings are involved it may also do String concatenation, that means sticking the two strings together. Examples:

There is a second addition operator, "+=". It increments the variable on the left of the += by the amount indicated on the right. It also works with Strings as a concatenation operator.

Subtraction
This subtracts two numbers, and is denoted by the "-" symbol. Examples:

Multiplication
This multiplies two numbers, and is denoted by the "*" symbol. Examples:

Division
There are more types of division than the one denoted by the "/" symbol. There is also integer division and remainder division.

Normal
This is the most commonly used form of division and is denoted by the "/" operator. Examples:

Integer division
This divides two numbers, and gives the result without the remainder if the quotient is a decimal. Examples:

Remainder Division
This divides two numbers, and gives the result's remainder if the quotient is a decimal. This is denoted by the operator "Mod." Examples:

Exponentiation
This is raising a number to a power. For example $$7^2$$ in VB .Net code is: This results in the number 49 being assigned to the variable x. It can also be used to calculate the square root of a number. The square root of a number is the number raised to the power of 0.5. Note: It is necessary to ensure that the variables be correctly declared to get the desired results. The following example works, but will produce the wrong result. This is because the Integer class does not allow decimal places (just like mathematical integers.)

Since x is declared as an Integer type, the value square root, a real number, is stored incorrectly.

Any nth root of number is the can be calculated by raising the number to the power of $$1/n$$: This is because $$x ^ \frac{1}{n} = \sqrt[n]{x}$$.

BODMAS
You have probably learnt about the order of operations in maths. BODMAS also applies to computer calculations. This means that when calculating a sum, the program will calculate:
 * 1) Brackets
 * 2) Order (powers n^2 etc)
 * 3) Division
 * 4) Multiplication
 * 5) Addition
 * 6) Subtraction

For example:

If in doubt use the brackets!