PPL notes Unit2
PPL notes Unit2
SYLLABUS CONTENT
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 1
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
Data type: A data type defines a collection of data values and a set of predefined
operations on those values.
● Some of the data types are merely reflections of the hardware - for eq. Integer
types
● Early PLs had only numeric primitive types, and still play a central role
among the collections of types supported by contemporary languages.
1. Numeric Types
a. Integer
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 2
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
b. Floating Point
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 3
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
c. Complex
● Represented as pair of real and imaginary part
● Imaginary Part is represented as literal such as i or j
● Ex. 5+7i is a complex number.
d. Decimal
Advantage
○ Accuracy
● Disadvantages:
○ Limited range
○ Wastes memory
2. Boolean Types
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 4
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
Advantage: Readability
3. Character Type
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 5
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
The basic (fundamental) data types provided by c++ are integral, floating point
and void data type.
Among these data types, the integral and floating-point data types can be
preceded by several type modifiers. These modifiers (also known as type qualifiers)
are the keywords that alter either size or range or both of the data types. The
various modifiers are short, long, signed and unsigned. By default the modifier is
signed.
In addition to these basic data types, ANSI C++ has introduced two more data
types namely, bool and wchar_t.
i. Integral Data Type: The integral data type is used to store integers and includes
char (character) and int (integer) data types.
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 6
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
b. Int: Numbers without the fractional part represent integer data. In C++, the int
data type is used to store integers such as 4, 42, 5233, -32, -745. The int data type
occupies 2 byte of memory
ii. Floating-point Data Type: A floating-point data type is used to store real numbers
such as 3 .28, 64. 755765, 8.01, -24.53. This data type includes float and double data
types. The float data type occupies 4 byte of memory , the double data type
occupies 8 byte of memory.
iii. Void:
● The void data type is used for specifying an empty parameter list to a
function and return type for a function.
● When void is used to specify an empty parameter list, it indicates that a function
does not take any arguments and when it is used as a return type for a
function, it indicates that a function does not return any value.
● For void, no memory is allocated and hence, it cannot store anything.
● As a result, void cannot be used to declare simple variables, however, it can
be used to declare generic pointers.
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 7
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
Data types that are derived from the built-in data types are known as derived
data types. The various derived data types provided by C++ are arrays, junctions,
references and pointers.
i. Array
An array is a set of elements of the same data type that are referred to by
the same name. All the elements in an array are stored at contiguous (one after
another) memory locations and each element is accessed by a unique index or
subscript value. The subscript value indicates the position of an element in an array.
iii. Reference A reference is an alternative name for a variable. That is, a reference
is an alias for a variable in a program. A variable and its reference can be used
interchangeably in a program as both refer to the same memory location. Hence,
changes made to any of them (say, a variable) are reflected in the other (on a
reference).
iv. Pointer A pointer is a variable that can store the memory address of another
variable. Pointers allow to use the memory dynamically. That is, with the help of
pointers, memory can be allocated or de-allocated to the variables at run-time, thus,
making a program more efficient.
i. Structure, Union and Class: Structure and union are the significant features of C
language. Structure and union provide a way to group similar or dissimilar data
types referred to by a single name. However, C++ has extended the concept of
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 8
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
structure and union by incorporating some new features in these data types to support
object -oriented programming.
C++ offers a new user-defined data type known as class, which forms the basis of
object-oriented programming. A class acts as a template which defines the data
and functions that are included in an object of a class. Classes are declared using
the keyword class. Once a class has been declared, its object can be easily created.
Data types specify the different sizes and values that can be stored in the variable.
There are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte,
short, int, long, float and double.
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 9
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
a. Boolean
The Boolean data type is used to store only two possible values: true and false. This data
type is used for simple flags that track true/false conditions.
The Boolean data type specifies one bit of information, but its "size" can't be defined
precisely.
Example:
Example:
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 10
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
The short data type is a 16-bit signed two's complement integer. Its value-range
lies between -32,768 to 32,767 (inclusive).
Example:
The int data type is a 32-bit signed two's complement integer. Its value-range lies
between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive).
Example:
The long data type is a 64-bit two's complement integer. Its value-range lies
between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)
(inclusive).
Example:
The float data type is a single-precision 32-bit IEEE 754 floating point. Its value
range is 3.4028 *1038 - 1.40239* 10-45
The float data type should never be used for precise values, such as currency. Its
default value is 0.0F.
Example:
float f1 = 234.5f
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 11
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
The double data type is a double-precision 64-bit IEEE 754 floating point.
Example:
double d1 = 12.3
The char data type is a single 16-bit Unicode character. Its value-range lies between '\
u0000' (or 0) to '\uffff' (or 65,535 inclusive).The char data type is used to store
characters.
Example:
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 12
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
● Static – Python, Java’s String class, C++ standard class library, Ruby’s built-in
String class, and the .NET class library in C# and F#.
● Limited dynamic length – C and C++ ( up to a max length indicated by a null
character)
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 13
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
● Limited Dynamic - may need a run-time descriptor for storing length (but
not in C and C++ because the end of a string is marked with the null character)
● First approach :
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 14
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
operations
● Second approach :
● Third approach:
○ To store complete strings in adjacent storage cells
○ When a string grows and adjacent storage is not available, a new area
of memory is found that can store the complete new string and the
old part is moved to this area, and the memory cells for the old
string are deallocated.
○ This results in faster string operations and requires less storage
An ordinal type is one in which the range of possible values can be easily associated
with the set of positive integers
1) Enumeration Type
2) Subrange
1) Enumeration
● All possible values, which are named constants, are provided in the
definition
● C# example
enum days {mon, tue, wed, thu, fri, sat, sun};
● The enumeration constants are typically implicitly assigned the integer
values, 0, 1, …, but can be explicitly assigned any integer literal in the
type’s definition
● In languages that do not have enumeration types, programmers
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 15
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
● Example
○ C++
➔ enum colors {red, blue, green, yellow, black};
➔ colors myColor = blue, yourColor = red;
➔ The enumeration values are coerced to int when they are
put in integer context.
➔ E.g. myColor++ would assign green to myColor.
○ Java
■ All enumeration types are implicitly subclasses of the
predefined class Enum. They can have instance data
fields, constructors and methods.
■ Enumeration days;
Vector dayNames = new Vector();
dayNames.add("Monday");
…
dayNames.add("Friday");
days = dayNames.elements();
while (days.hasMoreElements())
System.out.println(days.nextElement());
○ C# - Enumeration types are like those of C++ except that they
are never coerced to integer.
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 16
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
Problem:
Features
● Aid to readability, e.g., no need to code a color enum type as a number
● Aid to reliability, e.g., compiler can check: operations (don’t allow color enum
types to be added)
● No enumeration variable can be assigned a value outside its defined range,
○ e.g. if the colors type has 10 enumeration constants and uses 0 .. 9 as
its internal values, no number greater than 9 can be assigned to a
colors type variable.
● Ada, C#, and Java 5.0 provide better support for enumeration than C++
because enumeration type variables in these languages are not coerced
into integer types
2) Subrange
● An ordered contiguous subsequence of an ordinal type
● Not a new type, but a restricted existing type
● Example: 12..18 is a subrange of integer type
● Introduced by Pascal and included in Ada
● Ada’s design
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
subtype Weekdays is Days range Mon..Fri;
Features of SubRange Type
● Aid to readability
○ Make it clear to the readers that variables of subrange can store
only certain range of values
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 17
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
● Reliability
○ Assigning a value to a subrange variable that is outside the
specified range is detected as an error.
● Index Syntax
○ E.g. List(27) direct reference to the List array’s element with the subscript 27.
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 18
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
Ada Integer Or
Enumeration
● Fortran 95 defaults to 1
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 19
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
programmer
i) Static: subscript ranges are statically bound and storage allocation is static
(before run‐ time)
● Advantage: efficiency (no dynamic allocation/deallocation required)
● Example: In C and C++ arrays that include the static modifier are static
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 20
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
{
int fixed_stack_dynamic_array[7];
/* ... */
}
● Binding of subscript ranges and storage allocation is dynamic and can change
any number of times
● Advantage: flexibility (arrays can grow or shrink during program execution)
● Examples: Perl, JavaScript, Python, and Ruby support heap‐dynamic arrays
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 21
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
void foo(int n) {
int * heap_dynamic_array = malloc(n * sizeof(int));
}
void foo(int n)
heap_dynamic_array { int * heap_dynamic_array = malloc(n *
sizeof(int)); }
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 22
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 23
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 24
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
JAGGED ARRAY
A jagged array is one in which the lengths of the rows need not be the same. For
example, a jagged matrix may consist of three rows, one with 5 elements, one with 7
elements, and one with 12 elements. This also applies to the columns and higher
dimensions.
2.1.4.8 Slices
○ Python Declaration
■ A =[10,20,30,40,50,60]
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 25
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
■ A[1:4] [10,20,30]
■ A[:5] [10,20,30,40,50]
■ A[4:] [50,60]
● Example :
○ In Perl
%lookup =
("dave", 1234,
“peter", 3456,
"andrew", 6789);
A record is a collection of fields, possibly of different data types. Every field has
an identifier (field name) and a data type.
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 26
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
i. Records in C, C++, C# :
In C, C++, and C#, records are supported with the struct data type.
e.g.
Defining Linked List:
struct list
{
int info;
struct list *ptr;
};
ii. COBOL uses level numbers to show nested records; others use recursive definition
01 EMP-REC.
02 EMP-NAME.
iii. ADA
type Emp_Rec_Type is record
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 27
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
end record;
Emp_Rec: Emp_Rec_Type;
Access to Record Fields : Most Languages Use dot operator to access the fields of
record.
2.1.7 Union
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 28
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
https://www.comrevo.com/2021/04/record-types-ppl-sebesta-data-types-in-ppl.html
Blooms
Questions Marks
Level
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 29
Principles of Programming Languages
UNIT II. Structuring the Data, Computations and Program
SNJB’s KBJ COE, CHANDWAD, Department of Computer Engg Prepared by Kainjan M. Sanghavi | 30
Principles of Programming Languages