0% found this document useful (0 votes)
8 views3 pages

Topic 2 Notes

The document discusses the concept of data types in programming, explaining how each data item has a type that determines its value range and applicable operations. It covers primitive data types, such as enumeration types and standard types like INTEGER, REAL, BOOLEAN, and CHAR, detailing their characteristics and operations. Additionally, it highlights the importance of operators for computation and provides examples to illustrate the use of these data types in programming.

Uploaded by

antomotongori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Topic 2 Notes

The document discusses the concept of data types in programming, explaining how each data item has a type that determines its value range and applicable operations. It covers primitive data types, such as enumeration types and standard types like INTEGER, REAL, BOOLEAN, and CHAR, detailing their characteristics and operations. Additionally, it highlights the importance of operators for computation and provides examples to illustrate the use of these data types in programming.

Uploaded by

antomotongori
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Topic 2 Notes

2.1 The Concept of Data Type


Each data item has a type and a name chosen by the programmer.
The type determines the range of possible values it can hold as well as the operations that can be
used on it. For example, you can add a number to a numeric data type, but you cannot add a
number to someone's name. (What would "Joe" + 1 mean?)

2.2 Characteristics of the concept of type


1. A data type determines the set of values to which a constant belongs, or which may be assumed
by a variable or an expression, or which may be generated by an operator or a function.
2. The type of a value denoted by a constant, variable, or expression may be derived from its form
or its declaration without the necessity of executing the computational process.
3. Each operator or function expects arguments of a fixed type and yields a result of a fixed type.
If an operator admits arguments of several types (e.g., + is used for addition of both integers and
real numbers), then the type of the result can be determined from specific language rules.
Variables and data types are introduced in a program in order to be used for computation. To this
end, a set of operators must be available. For each standard data type a programming language
offers a certain set of primitive, standard operators, and likewise with each structuring method a
distinct operation and notation for selecting a component.
The most important basic operators are comparison and assignment, i.e., the test for equality (and
for order in the case of ordered types), and the command to enforce equality.
Test for equality: x = y (an expression with value TRUE or FALSE)
Assignment to x: X: = y (a statement making x equal to y)
These fundamental operators are defined for most data types, but it should be noted that their
execution may involve a substantial amount of computational effort, if the data are large and highly
structured.

2.3 Primitive Data Types


A new, primitive type is definable by enumerating the distinct values belonging to it. Such a type
is called an enumeration type. Its definition has the form:
TYPE T = (c1, c2, ... , cn)
T is the new type identifier, and the ci are the new constant identifiers.
Examples
TYPE shape = (rectangle, square, ellipse, circle)
TYPE color = (red, yellow, green)
TYPE sex = (male, female)
TYPE weekday = (Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday, Sunday)
The definition of such types introduces not only a new type identifier, but at the same time the set
of identifiers denoting the values of the new type. These identifiers may then be used as constants
throughout the program, and they enhance its understandability considerably. If, as an example,
we introduce variables
s, d, r, and b.
VAR s: sex
VAR d: weekday
VAR r: rank
then the following assignment statements are possible:
s := male
d := Sunday
r := major
b := TRUE

2.4 Standard Data Types


Standard primitive types are those types that are available on most computers as built-in features.
They include the whole numbers, the logical truth values, and a set of printable characters. On
many computers fractional numbers are also incorporated, together with the standard arithmetic
operations. We denote these types by the identifiers
INTEGER, REAL, BOOLEAN, CHAR, SET

Integer types
The type INTEGER comprises a subset of the whole numbers whose size may vary among
individual computer systems. It is assumed that all operations on data of this type are exact and
correspond to the ordinary laws of arithmetic, and that the computation will be interrupted in the
case of a result lying outside the representable subset. This event is called overflow.
The standard operators are the four basic arithmetic operations of addition (+), subtraction (-),
multiplication (*), and division (/, DIV).
Whereas the slash denotes ordinary division resulting in a value of type REAL, the operator DIV
denotes integer division resulting in a value of type INTEGER.

The type REAL


The type REAL denotes a subset of the real numbers. Whereas arithmetic with operands of the
types INTEGER is assumed to yield exact results, arithmetic on values of type REAL is permitted
to be inaccurate within the limits of round-off errors caused by computation on a finite number of
digits. This is the principal reason for the explicit distinction between the types INTEGER and
REAL, as it is made in most programming languages.
The standard operators are the four basic arithmetic operations of addition (+), subtraction (-),
multiplication (*), and division (/).

The type BOOLEAN


The two values of the standard type BOOLEAN are denoted by the identifiers TRUE and FALSE.
The Boolean operators are the logical conjunction, disjunction, and negation.
The logical conjunction is denoted by the symbol &, the logical disjunction by OR, and negation
by “~”.
Note that comparisons are operations yielding a result of type BOOLEAN. Thus, the result of a
comparison may be assigned to a variable, or it may be used as an operand of a logical operator in
a Boolean expression. For instance, given Boolean variables p and q and integer variables x = 5, y
= 8, z = 10, the two assignments
p := x = y
q := (x ≤ y) & (y < z)
yield p = FALSE and q = TRUE.
The exact definition of & and OR is therefore given by the following equations:
p & q = if p then q else FALSE
p OR q = if p then TRUE else q

The type CHAR


The standard type CHAR comprises a set of printable characters. Unfortunately, there is no
generally accepted standard character set used on all computer systems. Therefore, the use of the
predicate "standard" may in this case be almost misleading; it is to be understood in the sense of
"standard on the computer system on which a certain program is to be executed."
It consists of 95 printable (graphic) characters and 33 control characters, the latter mainly being
used in data transmission and for the control of printing equipment.
Properties of character sets include:
1. The type CHAR contains the 26 capital Latin letters, the 26 lower-case letters, the 10 decimal
digits, and a number of other graphic characters, such as punctuation marks.
2. The subsets of letters and digits are ordered and contiguous, i.e., 17("A"≤x) & (x≤"Z") implies
that x is a capital letter ("a"x) & (x"z") implies that x is a lower-case letter("0"x) & (x"9") implies
that x is a decimal digit
3. The type CHAR contains a non-printing, blank character and a line-end character that may be
used as separators.

Revision questions
1. The term data type
2. Differentiate between primitive and standard data types
3. What operations can be performed on character type
4. What are the items stored in Boolean type

You might also like