Documente Academic
Documente Profesional
Documente Cultură
Learning Objectives
To be exposed to the different type of data used in programming. To be able to differentiate the characteristics of each data type. To be able to give examples of each data type
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. 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.
Introduction (cont)
Most programming languages require the programmer to declare the data type of every data object. Most database systems require the user to specify the type of each data field.
Introduction (cont)
Data type refers to the structural format of the data used in the computer program. Data type provides the users with the flexibility of using any type of data while programming.
Integer
An integer is typically specified in the program 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
Examples
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 represented and it will depend on the size of memory allocated for that number. Commonly (and it may varies in lower and higher end computers) an integer when declared in a program will be allocated a size of 4 bytes 32 bits. 9
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 the value of : Max_int x min_int, if not we will have an error called overflow (value too large) or underflow (value too small).
10
Examples
What would be the maximum and minimum value of an integer if we have a word size of 8 bits?
11
Boolean
In computer science, the Boolean or logical data type is a data type, having two values (usually denoted true and false), intended to represent the truth values of logic and Boolean algebra.
12
Boolean (cont)
Comparison
Boolean (cont)
2<4 True 2 == 4 False 2>4 False 6.2 <= 6.2 True 6.2 <= 6.20001 True
Boolean (cont)
Operators and or not
We can use conjunction operations to chain together arbitrary expressions and logically combine the Boolean results: 2 < 4 and 2 == 4 False 2 > 4 or 2 < 4 True not 6.2 <= 6 True
Boolean (cont)
3<4<5 True
16
Example:
Selection control structure Compare two statements
17
Character
Character fields or can store text information that is not used in mathematical calculations, such as names, addresses, and numbers. For example, phone numbers or zip codes, though they include mostly numbers, are actually best stored as character values.
18
Character (cont)
A single character data is usually written in programs 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 in the computer as a numerical value based on the type of encoding it use (next chapter topic). Example of ASCII code table is provided as below:
19
20
21
22
23
24
Character (cont)
Character or char
6 L * m
25
Alphanumeric String
A string is a sequence of symbols that are 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 with double quotes, e.g. Hello there .. 123
26
Examples
Strings
CMPF134 $123.00 I
27
Floating point
Floating point describes a system for representing real numbers which supports a wide range of values. Numbers are in general represented approximately to a fixed number of significant digits and scaled using 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 the left 2 positions, and the exponent is based 10 as the number is in base 10 (decimal numbering system))
28
The term floating point refers to the fact that the radix point (decimal point) can "float"; that is, it can be placed anywhere relative to the significant digits of the number. This position is indicated separately in the internal representation, and floating-point representation can thus be thought of as a computer realization of scientific notation.
29
the most commonly encountered representation is that defined by the IEEE 754 Standard. (you will learn of this internal representation of a floating point number in your degree course) In our programs we may write floating point numbers as we usually do for real numbers e.g.
30
Example
31