FC308
Information
Communication
Technology
Theme: 5 Hour: 2 Algorithms – Logic and
Operators
Review
We can represent different ‘types’ of numbers and text in
computers using primitive, compound and abstract data
types.
We can express solutions to human problems using
algorithms (as either flowcharts or pseudocode)
But computers are logical machines, we need
to
express our solutions within a logical
framework…
In this session
Operators and Variables/Constants
Or
How computers work with data
Operators
3 categories:
Mathematical e.g.
10 + 4 Operator
Logical
e.g. a && b
Relational e.g.
Operand
a<b
Mathematical Operators
Operator Function int main() { // Some code in
C language
int a, b, c; // 1. Declare 3
= Assignment integers
x = 10; // 2. Assign
+ Adds two operands some values
y = 20;
- Subtracts second operand from
the first z = x + y; //
Do some maths
* Multiplies both operands printf( "Value of z = %d\n", z);
c = x - y;
/ Divides numerator by de- printf( "Value of z = %d\n", z);
numerator
c = x * y;
% This gives remainder of an printf( "Value of z = %d\n", z);
integer division
c = x / y;
printf( "Value of z = %d\n", z);
cOutput:
= x % y;
printf(Value ofofz z= =30%d\n", z);
"Value
Value of z = -10
} Value of z = 200
Value of z = 2
Value of z = 0
Further Mathematical Operators
Depending on the programming language and
environment other operators will be available,
possibly though functions e.g.
PI - Pi as a numerical
value
SqRt(x) - Get the square
root of x
Exp(x,y) - Raise x to the power of
y
Relational Operators
Take only 2 operands Some code in R.
> x <- 10
> y <- 20
Operator Symbol Description
> x==y
Equal == If the operands are equal the result is [1] FALSE
TRUE, otherwise FALSE
Greater than > If the 1st operand is greater than the 2nd
> x > y
the result is TRUE, otherwise FALSE [1] FALSE
Less than < If the 1st operand is less than the 2nd the > x < y
result is TRUE, otherwise FALSE [1] TRUE
Greater than or >= If the 1st operand is greater than OR equal > x >= y
Equal to the 2nd the result is TRUE, otherwise
FALSE [1] FALSE
Less than or <= If the 1st operand is less than OR equal to > x <= y
Equal the 2nd the result is TRUE, otherwise [1] TRUE
FALSE
> x != y
Not equal != , If the operands are different the result is
<> TRUE, otherwise FALSE [1] TRUE
Logical Operators
All computers are built from transistors that in turn make up
logic gates. The 3 fundamental gates are:
Description Programming Some code in R
Symbol
NOT IF the value is TRUE the ! > x <- TRUE
result is FALSE, otherwise > y <- FALSE
TRUE > !x
AND IF all values are TRUE the && [1] FALSE
result is TRUE, otherwise > !y
FALSE [1] TRUE
OR IF one or more of the value || > x&&y
are TRUE the result is TRUE,
[1] FALSE
otherwise FALSE
> x||y
[1] TRUE
Logical Operators
There are a further 4 derived operators;
Description
Exclusive If just one operand is TRUE the
OR (XOR) result is TRUE, otherwise FALSE
NAND The logical opposite of AND
NOR The logical opposite of OR
XNOR The logical opposite of XOR
Combining Operators
Operators can be combined or nested:
Mathematical Operators:
> x <- 15
> y <- 10
> z <- (3 * x + (y / 5)) - 1
> z
[1] 46
What are the rules for the order of execution?
Same as maths!
Logical Operators:
> x <- TRUE
> y <- FALSE
> z <- ! (x || (x && y))
> z
[1] FALSE
What are the rules for the order of execution?
NOT takes precedence then treat AND as multiply and OR as add
Variables
A value that can change
Has an associated datatype
Has a meaningful name
May be declared and/or defined before using
In C:
int age; Declare the name and define the type
age = 56; Assigns the value
In R:
age <- 56 Declares, defines (based on the value 56) and assigns
In Python:
age = 56 Declares, defines (based on the value 56) and assigns
Variables
Cannot de defined/declared more than once;
In C:
int age;
age = 56;
int age; This will cause an error as age is already defined
age = 72;
In R:
This is OK, but we overwrite the value of 56 with the
age <- 56 new value of 54
age <- 54
Variables
We will look at variables in more depth later in the module
though you can do further reading now; it will help with your
R assignment.
Constants/Literals
A value that does not change
Has an associated datatype
Has a meaningful name (usually in upper case)
Must be declared/defined
In C:
const int VAT_RATE = 1.20;
const string HEADER = “Name: Freda \n DOB: 30/11/1998”
In Python:
VAT_RATE = 1.2
HEADER = “Name: Freda \n DOB: 30/11/1998”
Note: Python/R are dynamic languages so it is possible to change constants! Use
capital letters to differentiate constants and variables.
Summary
Mathematical operators are used to change data values or create new
values based on existing data
Logical operators are used to compare or change Boolean data values
Relational operators are used to compare two data values
Algorithms should restrict themselves to using these fundamental
operators
Within programming variables and constants are used to hold data
values.
Next…
Control Structures