Arithmetic, Logical & Boolean Operators (OCR AS Computer Science): Revision Note
Exam code: H046
Arithmetic Operators
What are arithmetic operators?
Arithmetic operators are symbols or keywords used to perform mathematical calculations and operations on numerical values
They allow the computer to perform addition, subtraction, multiplication, division, and more
Addition operator (+)
The addition operator (
+) is used to add numerical values together or concatenate strings:
01 sum = 5 + 302 fullName = 'John' + ' ' + 'Doe'
In this example, the addition operator adds the values
5and3, resulting in8It also concatenates the strings
'John',' ', and'Doe'to form the full name'John Doe'
Subtraction operator (-)
The subtraction operator (
-) is used to subtract one numerical value from another:
01 difference = 10 - 4
In this example, the subtraction operator subtracts the value
4from10, resulting in6
Multiplication operator (*)
The multiplication operator (
*) is used to multiply numerical values together:
01 product = 3 * 5;
In this example, the multiplication operator multiplies the values
3and5, resulting in15
Exponentiation operator (^)
The exponentiation operator (
^) is used to multiply numerical values by the power of another number:
01 product = 3 ^ 3
In this example, the exponentiation operator multiplies the value
3to the power3 (3*3*3), resulting in27
The division operator (/)
The division operator (
/) is used to divide one numerical value by another:
01 quotient = 10 / 2
In this example, the division operator divides the value
10by2, resulting in5
The modulus operator (MOD)
The modulus operator (
MOD) returns the remainder when one numerical value is divided by another:
01 remainder = 10 MOD 3
In this example, the modulus operator divides the value
10by3and returns the remainder, which is1.3 will go into 10 a total of three times and there will be a remainder of 1
Operator precedence (BIDMAS / BODMAS)
Arithmetic operators follow the rules of operator precedence, which determine the order in which operators are evaluated. Parentheses
()can be used to control the order of evaluation:
01 result = 2 + 3 * 402 adjustedResult = (2 + 3) * 4
In the first example, without parentheses, the multiplication (
*) is performed before the addition (+), resulting in14In the second example, with parentheses, the addition is evaluated first, resulting in
5, which is then multiplied by4, resulting in20
Logical Operators
What are Logical Operators?
Logical or comparison operators are symbols or keywords used to compare values and return a boolean result. They allow a comparison of variables, or expressions to determine relationships, equality, or inequality between them.
Equal to (==) operator
The equal to operator (
==) compares two values and returnstrueif they are equal, andfalseotherwise
01 x = 502 y = 603 print(x == y)
In this example, the equal to operator compares the value
5with the value 6. These are not the same number sofalseis printed
Not equal to (!=) operator
The not equal to the operator (
!=) compares two values and returnstrueif they are not equal, andfalseif they are equal
01 x = 502 y = 703 print(x != y)
In this example, the not equal to operator compares the value
5with the value 7. As they're not equal,trueis printed
Greater than (>) and less than (<) operators
The greater than operator (
>) compares two values and returnstrueif the left operand is greater than the right operand. The less than operator (<) returnstrueif the left operand is less than the right operand
01 x = 502 y = 1003 print(x > y)04 print(x < y)
In this example, the greater than operator compares the value
5with the value10, resulting infalseThe less than operator compares
5with10, resulting intrue
Greater than or equal to (>=) and less than or equal to (<=) operators
The greater than or equal to operator (
>=) compares two values and returnstrueif the left operand is greater than or equal to the right operand. The less than or equal to operator (<=) returnstrueif the left operand is less than or equal to the right operand:
01 x = 502 y = 1003 print(x >= y)04 print(x <= y)
In this example, the greater than or equal operator compares
5with10, resulting infalseThe less than or equal to operator compares
5with10, resulting intrue
Boolean Operators
What are Boolean Operators?
Boolean operators are symbols or keywords used to combine and manipulate boolean values. They allow the computer to perform logical operations, such as combining conditions, negating values, and determining whether an expression is true or false.
AND operator
The AND operator returns
trueif both operands are true, otherwise returnsfalse:
01 x = 502 y = 1003 z = 1504 result = (x < y) AND (y < z)
In this example, the expression
(x < y) AND (y < z)evaluates totruebecause both conditions are true. If any of the conditions were false, the result would befalse
OR operator
The OR operator returns
trueif at least one of the operands is true, andfalseif both operands are false:
01 a = 502 b = 1003 c = 1504 result = (a > b) OR (b < c)
In this example, the expression
(a > b) OR (b < c)evaluates totruebecause the second condition is true, even though the first condition is false. If both conditions were false, the result would befalse
NOT operator
The NOT operator is used to negate a Boolean value. It returns
trueif the operand is false, andfalseif the operand is true:
01 value = false02 result = NOT value
In this example, the
notoperator negates the value offalse, resulting intrue
Unlock more, it's free!
Did this page help you?