成人论坛

Data types, structures and operators - EdexcelOperators

Programs use data, known as 鈥榲alues鈥. Variables hold values. Each variable in a program must have a data type. Sometimes a programmer needs to store a lot of related data. To do this they use structures such as arrays.

Part of Computer ScienceApplication of computational thinking

Operators

In computer science, an is a character or characters that determine the action that is to be performed or considered.

There are three types of operator that programmers use:

  • arithmetic operators
  • relational operators
  • logical operators

These operators are common to most .

Arithmetic operators

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

Arithmetic 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
Arithmetic operationAddition
Operator+
Examplex = x + 5
Arithmetic operationSubtraction
Operator-
Examplex = x - 5
Arithmetic operationMultiplication
Operator*
Examplex = x * 5
Arithmetic operationDivision
Operator/
Examplex = x / 5
Arithmetic operationInteger division
OperatorDIV
Examplex = x DIV 5
Arithmetic operationRemainder
OperatorMOD
Examplex = x MOD 5

Relational operators

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

Relational 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
Relational operationAssignment
Operator=
Examplex = 5
Relational operationEquivalence
Operator= or ==
Exampleif x = 5 or if x == 5
Relational operationLess than
Operator<
Exampleif x < 5
Relational operationLess than or equal to
Operator<=
Exampleif x <= 5
Relational operationGreater than
Operator>
Exampleif x > 5
Relational operationGreater than or equal to
Operator>=
Exampleif x >= 5
Relational operationDoes not equal
Operator<> or !=
ExampleIf x <> 5 or if x != 5

Logical operators

Logical operators are used to combine relational 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