You are on page 1of 27

CMPF134

Chapter 5 – Data Type


1
Chapter Objectives
◦ To be exposed to five basic types of data used in
computing world, especially programming
◦ To be able to differentiate the characteristics of each
data type
◦ To be able to provide appropriate examples for each
data type

2
Introduction
◦ It is easy for humans to distinguish between different types
of data. We can usually tell at a glance whether a number is a
percentage, a time, or an amount of money
◦ Unlike humans, a computer does not know the difference
between 1234, 12.34 and 12.34%
◦ Therefore, we usually need to tell the computer what type of
data it is. This is because the computer stores and processes
different types of data in different ways
3
Introduction (Cont.)
◦ In computer science, data types are closely related to variable
◦ A variable is a named location in memory where data can be stored
◦ Each variable should be given a well-defined type by the
programmer so that only data of the correct type are allowed to be
stored in that location
◦ Why do we need to define different variables’ data type?
◦ Different data types require varying amounts of memory storage allocation
◦ Different data types perform different computer operations
◦ Only accurate values are allowed to be stored in each respective variable
4
Introduction (Cont.)
◦ A data type in programming language refers to a set
of data with values having predefined characteristics. In other
words, it refers to the set of values that data of that type can
have
◦ Some programming languages require the programmer to
define the data type of a variable before assigning it a value
(example: C)
◦ Other languages can automatically assign a variable's data type
when the initial data is entered into the variable (example:
Python)
5
Basic Data Types
Five common data types are:
◦ Integer
◦ Floating point
◦ Boolean
◦ Character
◦ Alphanumeric string

6
Integer
◦ It is a sequence of digits, without spaces or thousand separators
(normally in decimal form)
◦ It can be either positive, negative or zero
◦ It is also known as whole number (number with no fractional or
decimal portion)
◦ Some programming languages allow alternative integer
notation, such as: hexadecimal (example: 0x2348, 0x978) or
octal (example: 032, 05732)
7
Integer (Cont.)
◦ Examples of valid integer values:
1. 988
2. -357
3. 48
◦ Examples of invalid integer values:
1. 1, 827
2. -17.35
3. $48

8
Integer Size
◦ In mathematic, number starts from 0, 1, 2,3…. and the list keeps
on going infinitely
◦ In a computer, only a finite portion of these numbers can be
represented and it depends on the size of memory allocated for
this number
◦ In general, a variable will be allocated with a memory size of 4
Bytes (32 bits), when it is declared with integer data type

9
Integer Size (Cont.)
◦ The range of integer values accepted in a variable depends on
the number of bits (n) that has been allocated for it:
 Maximum integer value, max_int = (2n−1)−1
 Minimum integer value, min_int = −(2n−1)
◦ So, all integer variable (let’s say X) should carry the value
within the range of max_int >= X >= min_int, or we will
get error called “Overflow” (integer value too big) or
“Underflow” (integer value too small)
10
Exercise 1
Identify the maximum and minimum integer values allowed for a
variable with the word size of 4 bits.

Solution:
Given n = 4, therefore:
◦ max_int = (2n−1)−1 = (24−1) – 1 = 23 – 1 = 7
◦ min_int = −(2n−1) = −(24−1) = −(23) = -8

11
Exercise 2
What would be the accepted integer value range for a variable, P,
with 2 Bytes word length?

Solution:
Given n = 16 (2 Bytes * 8), therefore:
◦ max_int = (2n−1)−1 = (216−1) – 1 = 215 – 1 = 32767
◦ min_int = −(2n−1) = −(216−1) = −(215) = -32768
◦ Integer value range:
◦ max_int >= P >= min_int
◦ 32767 >= P >= -32768
12
Floating Point
◦ It is used to represent real numbers (a number that can contain
a fractional part), example: 0.0357
◦ Floating-point numbers can also be expressed in scientific
notation, example: 3.57 x 10-2 @ 3.57e-2
◦ The value before the letter e is known as the mantissa (3.57)
◦ whereas the value after letter e is called the exponent (-2). It can be
preceded by an optional plus or minus sign, represents the power of 10
by which the mantissa is to be multiplied

13
Parts of a Floating Point Number
Location of
decimal point

-3.257 x 10 -3
Exponent

Sign of Mantissa Sign of exponent


mantissa

Base

14
Floating Point (Cont.)
◦ The term floating point is derived from the fact that there is no
fixed number of digits before and after the decimal point; that is,
the decimal point can float
◦ Example: 0.0357 = 0.357 x 10-1 = 3.57 x 10-2 = 35.7 x 10-3
◦ IEEE754 standard: Most common standard for representing
floating point numbers in computer (you will learn in more
detailed in degree course)

15
Floating Point (Cont.)
◦ Examples of valid floating point numbers:
1. 12.58
2. 357.13
3. 0.48
4. 0.058
5. 5.8 x 10-2

16
Boolean
◦ Named after George Boole (1815-1864), who invented
mathematical logic and defined Boolean algebra
◦ Variables of bool type can take only one of two possible values:
true (True, TRUE, 1) or false (False, FALSE, 0)
◦ Example:
 result = True
 final_decision = 1

17
Boolean (Cont.)
◦ This data type is used for simple flags that track true or false
conditions (example: in selection control structure and repetition
control structure)
◦ example:

if age >= 55 then while number <> 5 then


print “Retired” print number
else else
print “Go back to work!!” print “Bye-bye”

18
Boolean (Cont.)
◦ It is also used to represent true and false values returned by
Boolean expressions which use either relational operators or
logical operators

19
Relational Operator
Operator Computer Example of Operation Boolean Value
Symbol

Equal to == 3 == 7 False

Less than < 3<7 True

Greater than > 3>7 False

Lesser than or equal to <= 3 <= 7 True

Greater than or equal to >= 3 >= 7 False

Not equal to <> , != 3 <> 7 , 3 != 7 True

20
Logical Operator
Operator Computer Example of Operation Boolean Value
Symbol

Not not , ! not True False


Hierarchy of
And and , && True && False False
operation
Or or , || True or False True

Depends on type of programming language

21
Exercise 3
Evaluate the Boolean value of variable R. Given the values A = 5,
B = 2, C = True, D = False.
1. R = A > B && C || D
2. R = A <> B or not C and D
3. R = !(B <= A) && C || D
4. R = not(B != A) or C and D

22
Character
◦ This data type is used when we want a variable which can store
letters, digits, punctuation marks, blank space and control
characters
◦ Control character: a character that does not represent a printable character
but serves to initiate a particular action
◦ Every character is represented by a computer code (integer
number), such as:
◦ American Standard Code for Information Interchange (ASCII) – represents a
character in 8 bits form
◦ Unicode - represents a character in 16 bits form
23
Character (Cont.)
◦ A character type data is usually enclosed by single quotes (again, it
depends on programming language’s syntax)
◦ Example :
item_code = ‘A’
◦ Data value of this type cannot be used in mathematical calculations
◦ Example of valid character data :
1. ‘6’
2. ‘A’
3. ‘*’

24
Alphanumeric String
◦ In computer science, an alphanumeric string is any finite
sequence of characters (i.e., letters, numerals, symbols,
punctuation marks and spaces)
◦ A string will contain more than one character and they are usually
written in between a pair of double quote (“ ”)
◦ String data is stored in memory in the form of arrays
◦ While storing string data, one extra null character ‘\0’ is always
attached at the end of data. Therefore, if a 5 character string data
is stored in memory, 6 spaces will be occupied
25
Alphanumeric String (Cont.)
◦ Example of valid alphanumeric string data:
1. “I am having so much fun learning CMPF134!!”
2. “$125.50”
3. “AT92485”

26
Exercise 4
Identify the suitability of data type assigned to each variable
below. Support your answer with proper justification.
1. my_result = “True” data type = Boolean
2. my_score = 3.00 data type = integer
3. total_student = 1,358 data type = integer
4. price_code = ‘A’ data type = string
5. final_result = False data type = string
6. phone_number = 0123456789 data type = integer
7. favourite_number = ‘8’ data type = integer
27

You might also like