Programming fundamentals - OCRApplying computer-related mathematics

Programs are designed using common building blocks, known as programming constructs. These programming constructs form the basis for all programs.

Part ofComputer ScienceComputational thinking, algorithms and programming

Applying computer-related mathematics

Mathematical operators

Computers are designed to carry out calculations. Mathematical operators allow arithmetic to be performed on values:

Mathematical operationOperatorExample
Addition+x = x + 5
Subtraction-x = x - 5
Multiplication*x = x * 5
Division/x = x / 5
Integer divisionDIVx = x DIV 5
RemainderMODx = x MOD 5
Exponentiation^x = x^2
Mathematical operationAddition
Operator+
Examplex = x + 5
Mathematical operationSubtraction
Operator-
Examplex = x - 5
Mathematical operationMultiplication
Operator*
Examplex = x * 5
Mathematical operationDivision
Operator/
Examplex = x / 5
Mathematical operationInteger division
OperatorDIV
Examplex = x DIV 5
Mathematical operationRemainder
OperatorMOD
Examplex = x MOD 5
Mathematical operationExponentiation
Operator^
Examplex = x^2

Calculations carried out by computers follow the same principles used when working out sums manually.

Addition

Addition uses the ‘+’ symbol:

6 + 6

x + y

x = x + 2

x = x + y

Subtraction

Subtraction uses the ‘-’ symbol:

8 - 6

x - y

x = x - 2

x = x - y

Multiplication

Multiplication uses the ‘*’ symbol:

5 * 5

x * y

x = x * 5

x = x * y

Division

Division uses the ‘/’ symbol:

6 / 2

x / y

x = x / 2

x = x / y

Modulus

Modulus gives the remainder from a division calculation:

7 MOD 3 = 1 (7 / 3 = 2 remainder 1)

Many programming languages, such as Python, use the ‘%’ character to represent modulus:

6 % 2

x % y

x = x % 2

x = x % y

Integer division

Integer division (also known as floor division) discards the remainder:

7 DIV 3 = 2 (7 / 3 = 2 remainder 1 – the remainder is discarded)

Some programming languages, such as Python, use ‘//’ to represent integer division:

6 // 3

x // y

x = x // 2

x = x // y

Exponentiation

Exponentiation uses powers represented by the symbol ‘^’:

2 ^ 2

= 2 x 2

= 4

2 ^ 3

= 2 x 2 x 2

= 8

2 ^ 4

= 2 x 2 x 2 x 2

= 16

Some programming languages, such as Python, use ‘**’ to represent exponentiation:

2 ** 2

x ** 2

x ** y

Use of brackets

Calculations within brackets are performed first. If there are no brackets, multiplication is performed first, then division, addition and finally, subtraction:

For example:

(4 + 2) * 5

= 6 * 5

= 30

Whereas:

4 + 2 * 5

= 4 + 10

= 14

4 - 2 / 2

= 4 - 1

= 3

4 * 2 / 4

= 8 / 4

= 2

4 + 2 - 3

= 6 - 3

= 3

Where multiple brackets exist, the inside brackets are dealt with first:

((4 / 2) + (3 * 3)) + 2

= (2 + 9) + 2

= 11 + 2

= 13