Please enable JavaScript.
Coggle requires JavaScript to display documents.
Computer Science Chapter 9 (Common Arithmetic Operators (Addition (Adds…
Computer Science Chapter 9
Data Types and Casting
Different data types
Integer
A whole number, negative or positive
Only use for data that requires a calculation
Float (real)
A number with a decimal place
Only use for data that requires a calculation
Boolean (flag)
True or false
Used to track whether something has happened or not
Character
A single alphanumeric character
Only use for data that does not require a calculation
String
A set of more than one alphanumeric character
Only use for data that does not require a calculation
How to cast
X = Str(x) casts the variable x to a string
X = Int(x) casts the variable x to an integer
Optimisation
Each data type uses a different amount of memory
Therefore to optimise the program, the correct data type must be used
Casting
To manipulate integers, we may need them to be in strings
Functions will return data in a particular type
Changing data types is needed in order to make a program useful
Changing data types is known as
casting
Casting allows numbers to be manipulated as strings
It means that sub-problems can receive data in the form that they are expecting it
It allows inputs that are always strings to become numbers
In some programming languages, the data type of a variable may need to be determined before it can be used
Variables and Constants
Definitions
Variable - A value stored in memory whilst the program is running
Constant - A variable that doesn’t change throughout the program
Assignment - When a number is given to a variable
Input - A value that is read from an input device
Casting - Converting a variable from one data type to another
Advantages of constants
They can make a program easier to read
They can be easily be changed by the programmer in one place, instead of changing every time they occur, leading to less errors
The computer can optimise the code making the program faster, if you use
Why casting is necessary
Casting changes one data type to another
Inputs from keyboards are always characters, multiple characters are strings
However to perform a calculation the ALU must have numbers, so casting is necessary
1 as a character is 00110001 but as a number is 00000001
Integers use less memory than floats, so using an int where possible
Common Arithmetic Operators
Addition
Adds numbers together
7 + 2 = 9
Subtraction
Subtracts numbers
7 - 2 = 5
Multiplication
7 * 2 = 14
Multiplies numbers together
Division
Divides numbers
7 / 2 = 3.5
Exponentiation
‘To the power of...’
7 ^ 2 = 49
Modulus
Calculates the remainder of an integer division
7 mod 2 = 1
Integer division
Divides numbers with no decimal places
7 div 2 = 3
Boolean and Comparison Operators
Boolean
There are 3 common Boolean operators: AND, OR, NOT
Comparison
Comparison operators will always be true or false