In the majority of cases, variables will be used in a program to calculate a meaningful result from the user’s input.
In programming, the key arithmetic operators are +, – , * and /.
We will examine another arithmetic operator that is referred to as mod or modulus.
Operator Operation Example
+ Addition result = 6 + 3 (= 9)
- Subtraction result 6 - 3 (= 3)
* Multiplication result = 6 * 3 (= 18)
/ Real Division result = 6 / 3 (= 2)
% Integer Division (Mod) result = 6 % 3 (= 0)
result = 7 % 3 (= 1)
Note: The integer division (mod) operator calculates and returns the remainder of the calculation with the quotient part being discarded. For example, if the calculation was 5 % 2 this will give a quotient of 2 with a remainder of 1, that is 2 goes into 5 twice but leaves the remainder of 1.
The Double Life of +
As well as being used to carry out additions on numeric data the + operator can also be used to add strings and characters together. When it is used in this way it is called concatenation, when is basically a fancy word that means joining two things together, for example, the expression FullName = ‘Ada’ + ‘Lovelace’ would assign the value ‘AdaLovelace’ to the FullName variable. If a space is required between the two names it can be added as well, for example, FullName = ‘Charles’ + ‘ ‘ + ‘Babbage’. Which would result in the value of ‘Charles Babbage’.