Programming fundamentals - OCRCommon arithmetic operators

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

Common arithmetic operators

An operator is a character, or characters, that determine what action is to be performed or considered.

There are three types of operator that programmers use:

  • mathematical operators
  • comparison operators
  • logical operators

These operators are common to most high level programming languages.

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
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

Comparison operators

Comparison operators allow for assignment and for comparisons to be made. They are used in condition testing.

Comparison operationOperatorExample
Assignment=x = 5
Equivalence= or ==if x = 5 or if x == 5
Less than<if x < 5
Less than or equal to<=if x <= 5
Greater than>if x > 5
Greater than or equal to>=if x >= 5
Does not equal<> or !=If x <> 5 or if x != 5
Comparison operationAssignment
Operator=
Examplex = 5
Comparison operationEquivalence
Operator= or ==
Exampleif x = 5 or if x == 5
Comparison operationLess than
Operator<
Exampleif x < 5
Comparison operationLess than or equal to
Operator<=
Exampleif x <= 5
Comparison operationGreater than
Operator>
Exampleif x > 5
Comparison operationGreater than or equal to
Operator>=
Exampleif x >= 5
Comparison operationDoes not equal
Operator<> or !=
ExampleIf x <> 5 or if x != 5

Logical operators

Logical operators are used to combine logical operators to give more complex decisions.

Logical operationOperatorExample
AndANDif x > 0 AND x < 10
OrORif topic == "Computing" OR topic == "Computer Science"
NotNOTwhile NOT x
Logical operationAnd
OperatorAND
Exampleif x > 0 AND x < 10
Logical operationOr
OperatorOR
Exampleif topic == "Computing" OR topic == "Computer Science"
Logical operationNot
OperatorNOT
Examplewhile NOT x

You can find out more about Boolean algebra in the computational logic study guide.