Algorithms - EduqasOperators

Algorithms are step-by-step plans for solving problems. They are a starting point when writing a program. Algorithms can be designed using pseudo-code and flowcharts.

Part ofComputer ScienceUnderstanding Computer Science

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:

Mathematical operations

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 division
DIV
(finds the whole number after the division)
x = x DIV 5 
If
x
is 21, then the result of this is that
x
is 4
Remainder
MOD
(finds the remainder after the modulus division)
x = x MOD 5 
If
x
is 21, then the result of this is that
x
is 1
Mathematical operationAddition
Operator
+
Example
x = x + 5
Mathematical operationSubtraction
Operator
-
Example
x = x - 5
Mathematical operationMultiplication
Operator
*
Example
x = x * 5
Mathematical operationDivision
Operator
/
Example
x = x / 5
Mathematical operationInteger division
Operator
DIV
(finds the whole number after the division)
Example
x = x DIV 5 
If
x
is 21, then the result of this is that
x
is 4
Mathematical operationRemainder
Operator
MOD
(finds the remainder after the modulus division)
Example
x = x MOD 5 
If
x
is 21, then the result of this is that
x
is 1

Logical operators

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

Logical operationOperatorExample
Equivalence
==
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
<>
if x <> 5
Logical operationEquivalence
Operator
==
Example
if x == 5
Logical operationLess than
Operator
<
Example
if x < 5
Logical operationLess than or equal to
Operator
<=
Example
if x <= 5
Logical operationGreater than
Operator
>
Example
if x > 5
Logical operationGreater than or equal to
Operator
>=
Example
if x >= 5
Logical operationDoes not equal
Operator
<>
Example
if x <> 5

Boolean operators

Boolean operators are used to connect and compare the relationship between arguments. The result will be either TRUE or FALSE.

Boolean operationOperatorExample
Both statements must be true for the argument as a whole to be true.
AND
if x>=5 AND x <=20
Returns
TRUE
if
x
is any number between 5 and 20.
Only one of the statements needs be true for the argument as a whole to be true.
OR
if x==2 OR x==5
Returns
TRUE
if
x
is either 2 or 5.
The opposite of the argument is true.
NOT
if NOT(x==10)
Returns
TRUE
if
x
is not 10.
The argument is false if both statements are true. The argument is false if both statements are false. Otherwise the argument is true.
XOR
if x<=10 XOR y<=10
Returns
TRUE
if one of
x
or
y
is greater than 10 and the other is not.
Boolean operationBoth statements must be true for the argument as a whole to be true.
Operator
AND
Example
if x>=5 AND x <=20
Returns
TRUE
if
x
is any number between 5 and 20.
Boolean operationOnly one of the statements needs be true for the argument as a whole to be true.
Operator
OR
Example
if x==2 OR x==5
Returns
TRUE
if
x
is either 2 or 5.
Boolean operationThe opposite of the argument is true.
Operator
NOT
Example
if NOT(x==10)
Returns
TRUE
if
x
is not 10.
Boolean operationThe argument is false if both statements are true. The argument is false if both statements are false. Otherwise the argument is true.
Operator
XOR
Example
if x<=10 XOR y<=10
Returns
TRUE
if one of
x
or
y
is greater than 10 and the other is not.

These can also be combined, for example:

If (x AND y) OR (NOT C)

Truth tables

Truth tables are a way of showing all the possible outputs for inputs in a logical expression. Logic gates, used in electronics and therefore computer circuits, are based on truth tables. More complex expressions can combine several inputs and outputs. In the following tables, 1 means TRUE and 0 means FALSE.

AND truth table

ABOutput
000
010
100
111
A0
B0
Output0
A0
B1
Output0
A1
B0
Output0
A1
B1
Output1

In an AND truth table, the output is only TRUE if both inputs are also TRUE.

OR truth table

ABOutput
000
011
101
111
A0
B0
Output0
A0
B1
Output1
A1
B0
Output1
A1
B1
Output1

In an OR truth table, the output is TRUE if any of the inputs are TRUE.

NOT truth table

AOutput
01
10
A0
Output1
A1
Output0

A NOT truth table reverses the input value.

XOR truth table

ABOutput
000
011
101
110
A0
B0
Output0
A0
B1
Output1
A1
B0
Output1
A1
B1
Output0

In an XOR truth table, the output is TRUE if only one of the input values is TRUE.

Boolean notation

Logical expressions can also be expressed using algebra notation. Instead of writing the words AND, OR, NOT or XOR it is often written using the following shorthand notation.

Boolean expressionShorthand notation
A AND BA . B
A OR BA + B
NOT AA’ (can be also shown as Ā)
A XOR BA ⊕ B
Boolean expressionA AND B
Shorthand notationA . B
Boolean expressionA OR B
Shorthand notationA + B
Boolean expressionNOT A
Shorthand notationA’ (can be also shown as Ā)
Boolean expressionA XOR B
Shorthand notationA ⊕ B

Boolean identities and rules

RuleAND formOR form
Commutative lawA . B = B . AA + B = B + A
Associative law(A . B) . C = A . (B . C)(A + B) + C = A + (B + C)
Distributive law(A . B) + C = (A + C) . (B + C)(A + B) . C = (A . C) + (B . C)
Identity lawA . 1 = AA + 0 = A
Zero and one lawA . 0 = 0A + 1 = 1
Inverse lawA . A’ = 0A + A’ = 1
Idempotent lawA . A = AA + A = A
Absorption lawA . (A + B) = AA + A . B = A A + A’ B = A + B
Double complement lawĀ = A
RuleCommutative law
AND formA . B = B . A
OR formA + B = B + A
RuleAssociative law
AND form(A . B) . C = A . (B . C)
OR form(A + B) + C = A + (B + C)
RuleDistributive law
AND form(A . B) + C = (A + C) . (B + C)
OR form(A + B) . C = (A . C) + (B . C)
RuleIdentity law
AND formA . 1 = A
OR formA + 0 = A
RuleZero and one law
AND formA . 0 = 0
OR formA + 1 = 1
RuleInverse law
AND formA . A’ = 0
OR formA + A’ = 1
RuleIdempotent law
AND formA . A = A
OR formA + A = A
RuleAbsorption law
AND formA . (A + B) = A
OR formA + A . B = A A + A’ B = A + B
RuleDouble complement law
AND formĀ = A

Examples:

A AND A = A

How?

If A is 0, then the expression is 0 AND 0 = 0, which is equal to A.

If A is 1, then the expression is 1 AND 1 = 1, which is equal to A.

A OR A = A

How?

If A is 0, then the expression is 0 OR 0 = 0, which is equal to A.

If A is 1, then the expression is 1 OR 1 = 1, which is equal to A.

A AND NOT A = 0

How?

If A is 1, then NOT A = 0.

Then the expression becomes 1 AND 0 = 0.

If A is 0, then NOT A = 1.

Then the expression becomes 0 AND 1 = 0.

Simplifying Boolean expressions

You may be asked to simplify a Boolean expression, for example:

X = A AND B OR A AND NOT B

X = A AND (B OR NOT B)

X = A AND 1

X = A