chapter 5 data type
TRANSCRIPT
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 1/31
CMPF134: Fundamentalsof Data and Information
Chapter 5Data Types
1
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 2/31
Learning Objectives
To be exposed to the different type of dataused in programming.
To be able to differentiate the characteristicsof each data type.
To be able to give examples of each datatype
2
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 3/31
Introduction
It is easy for humans to distinguishbetween different types of data.
We can usually tell at a glance whether anumber is a percentage, a time, or an
amount of money. However, we need to define data type in
computing system because the data type
defines: the amount of storage allocated to variables.
the values that they can accept.
the operations that can be performed on
variables. 3
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 4/31
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 5/31
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 6/31
Basic Data Types
FIVE common data types include: Integers
Boolean
characters floating-point numbers
alphanumeric strings.
6
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 7/31
Integer
An integer is typically specified in theprogram as a sequence of digits,without spaces or thousands
separators, optionally prefixed with +or -.
It is also what commonly we refer as
the whole number. Some programming languages allow
alternative notations, i.e hexadecimal(base 16) or octal (base 8).
7
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 8/31
Examples
Valid integer values 1009
321
9999999 Invalid integer values:
1,009
321.0 $999999
8
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 9/31
Integer sizes
In math natural number are 0, 1, 2,….. and the list goes on infinitely.
In a machine only a finite portion of
these numbers can be representedand it will depend on the size of memory allocated for that number.
Commonly (and it may varies in lowerand higher end computers) an integerwhen declared in a program will beallocated a size of 4 bytes – 32 bits.
9
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 10/31
Range of integer values
The maximum (Max_int) and minimum (Min_int)value of an integer will depend on the number of bits (n) that is allocated for it.
Max_int = 2n-1 -1
Min_int = - 2n-1
For a 32 bits (4 bytes) sized word:
Max_int = 231 -1 = +2147483647
Min_int = - 231 = -2147483648
So all integer variable (e.g. x) should carry thevalue of : Max_int ≥ x ≥ min_int, if not we willhave an error called overflow (value too large) orunderflow (value too small).
10
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 11/31
Examples
On machine with 2-byte word length(16 bits)
Max_int = +32767
Min_int = -32768
What would be the maximum and
minimum value of an integer if wehave a word size of 8 bits?
11
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 12/31
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 13/31
Boolean (cont)
Comparison operators return a Boolean valueindicating the truthfulness of the expression.
Operators(symbol)
<
<=
>
>=
==!=
<>
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 14/31
Boolean (cont)
2 < 4True
2 == 4
False2 > 4
False
6.2 <= 6.2True
6.2 <= 6.20001
True
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 15/31
Boolean (cont)Operators
and
or
not
•
We can use conjunction operations to chain togetherarbitrary expressions and logically combine the Boolean
results:
2 < 4 and 2 == 4
False
2 > 4 or 2 < 4
True
not 6.2 <= 6
True
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 16/31
Boolean (cont)
3 < 4 < 5True
3 < 4 < 5 is a short way of saying3<4 and 4<5
16
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 17/31
Example:
Selection control structure Compare two statements
17
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 18/31
Character
Character fields or can store textinformation that is not used inmathematical calculations, such as
names, addresses, and numbers. For example, phone numbers or zip
codes, though they include mostly
numbers, are actually best stored ascharacter values.
18
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 19/31
Character (cont)
A single character data is usually written inprograms with a single quote e.g. „a‟, „D‟,
„5‟, „&‟ etc.
It can be any digit, blank space,
punctuation mark, etc
Each of the character will be represented inthe computer as a numerical value based
on the type of encoding it use (nextchapter topic).
Example of ASCII code table is provided asbelow:
19
ASCII d t bl F t f
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 20/31
ASCII codes table - Format of standard characters
20
ASCII d t bl F t f
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 21/31
ASCII codes table - Format of standard characters (cont)
21
ASCII d t bl F t f
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 22/31
ASCII codes table - Format of standard characters (cont)
22
ASCII d t bl F t f
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 23/31
ASCII codes table - Format of standard characters (cont)
23
ASCII d t bl F t f
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 24/31
ASCII codes table - Format of standard characters (cont)
24
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 25/31
Character (cont)
Character or char „6‟
„L‟
„*‟
„m‟
25
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 26/31
Alphanumeric String
A string is a sequence of symbols thatare chosen from a set of alphabet,number or blank space, punctuation
mark, etc A string will contain more than one
character and are usually written withdouble quotes, e.g. “Hello there ..123”
26
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 27/31
Examples
Strings “CMPF134”
“$123.00”
“I love studying in Uniten”
27
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 28/31
Floating point
Floating point describes a system for representingreal numbers which supports a wide range of values.
Numbers are in general represented approximatelyto a fixed number of significant digits and scaledusing an exponent. Significant digits × base exponent , in the scientific notation form.
E.g. 934.5678 can be represented as
9.345678 x 102
(since the actual decimal point has been moved to theleft 2 positions, and the exponent is based 10 as thenumber is in base 10 (decimal numbering system))
28
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 29/31
Floating point (cont)
The term floating point refers to the factthat the radix point (decimal point) can"float"; that is, it can be placed anywhererelative to the significant digits of the
number. This position is indicated separately in the
internal representation, and floating-pointrepresentation can thus be thought of as acomputer realization of scientific notation.
29
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 30/31
Floating point (cont)
the most commonly encountered representation isthat defined by the IEEE 754 Standard. (you willlearn of this internal representation of a floatingpoint number in your degree course)
In our programs we may write floating pointnumbers as we usually do for real numbers e.g.
569.001, 0.6780 etc. (no need in scientific notation)
30
7/31/2019 Chapter 5 Data Type
http://slidepdf.com/reader/full/chapter-5-data-type 31/31
Example
Floating point numbers: 12.55
569.001
0.6780 934.5678
9.345678 x 102
31