Unit 2 - Principles of Programming Languages - Www.rgpvnotes.in
Unit 2 - Principles of Programming Languages - Www.rgpvnotes.in
Subject Notes
Department of Computer Science and Engineering
Subject Name: PPL Subject Code:CS6002
Unit-2
Data type:
A data type defines a collection of data values and a set of predefined operations on those values.
Design issues for all data types
i. What operations are defined and how are they specified.
ii. It is convenient, both logically and concurentely, to think of variables in terms of descriptors.
Descriptor:
A descriptor is the collection of the attributes of a variable
i. A descriptor is used for type checking, allocation and, de-allocation.
ii. Static attributes need only be available at compile-time; dynamic attributes need to be available at
run-time.
Exponent Fraction
Sign bit
Figure 2.1 (a) Single Precision floating point format
11 bits 52 bits
Exponent Fraction
Sign bit
C. Decimal
i. for business applications.
ii. store a fixed number of decimal digits (coded)
Advantage: accuracy
Disadvantages: limited range, wastes memory
D. Boolean types
i. The range of values has only two elements TRUE or FALSE.
ii. Booleans types are often used to represent switches or flags in programs
Character- Character is a symbol in programming language that has meaning. A character can be any letter,
number, punctuation marks, symbols or whitespace. For example, the word "character" consists of eight
characters and the phrase "Hello World!" consists of 12 characters including the whitespace and exclamation
mark. In programming character is a datatype. We can declare a variable as of a character type and store
characters in the variable. For example, in C and Java, we write,
Characters when storing in a variable must be inserted between single quotes. String is another datatype in
programming, which is a modified version of character datatype. Strings are used to store more than one
character
Character String Types
Character string type is one in which the values consist of sequences of characters
Design issues with the string types
i. Should strings be simply a special kind of character array or a primitive type?
ii. Should strings have static or dynamic length?
String Operations
i. Assignment ( Java: str1 = str2;) (C: strcpy(pstr1, pstr2);
ii. Comparison (=, >, etc.) BASIC: str1 < str2
iii. Concatenation, C: strcat (str1,str2), (Java : str2 + str3;)
iv. Substring reference
v. Pattern matching, C: strcmp(str1,str2);
Implementation
i. Static length - compile-time descriptor
ii. Limited dynamic length - may need a run-time descriptor for length (but not in C and C++ because the
end of a string is marked with the null character)
iii. Dynamic length - need run-time descriptor; allocation/deallocation is the biggest implementation
problem
Limited dynamic string
Length
Current Length
Address Address
Figure 2.2 Compile and Run time Descriptor for static and dynamic string
Array
Array is a data structure, which provides the facility to store a collection of data of same type under single
variable name. Just like the ordinary variable, the array should also be declared properly. The declaration of
array includes the type of array that is the type of value we are going to store in it, the array name and
maximum number of elements.
Design Issues
i. What types are legal for subscripts?
ii. Are subscripting expressions in element references range checked?
iii. When are subscript ranges bound?
iv. When does allocation take place?
v. Are Jagged or rectangular multidimensioned arrays allowed, or both?
vi. Can array objects be initialized?
vii. Are any kind of slices allowed?
Associative
i. An associative array is an unordered collection of data elements that are indexed by an equal number
of values called keys.
ii. Also known as Hash tables
a. Index by key (part of data) rather than value.
b. Store both key and value (take more space).
c. Best when access is by data rather than index.
iii. Examples:
Lisp alist: ((key1 . data1) (key2 . data2) (key3 . data3)
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Associative arrays have an index that is not necessarily an integer, and can be sparsely populated. The index
for an associative array is called the key, and its type is called the Key Type.
Design Issues
i. What is the form of references to elements?
ii. Is the size static or dynamic?
Record
A record is a possibly heterogeneous aggregate of data elements in which the individual elements are
identified by names
Design Issues
i. What is the form of references?
ii. What unit operations are defined? (Assignment, equality, assign corresponding filed)
4. Record Operations
i. Assignment
a. Pascal, Ada, and C allow it if the types are identical
b. In Ada, the RHS can be an aggregate constant
ii. Initialization
Allowed in Ada, using an aggregate constant
iii. Comparison
In Ada, = and /=; one operand can be an aggregate constant
iv. Move Corresponding
a. In COBOL - it moves all fields in the source record to fields with the same names in the destination
record
b. Useful operation in data processing application, where input records are moved to output files after
same modification.
Comparing records and arrays
a. Access to array elements is much slower than access to record fields, because subscripts are dynamic
(field names are static)
b. Dynamic subscripts could be used with record field access, but it would disallow type checking and it
would be much slower.
Record
Name
Field 1
Type
Offset
.
:
.
:
Name
Type
Field n Offset
Address
Union
Union is a data type with two or more member similar to structure but in this case all the members share a
common memory location. The size of the union corresponds to the length of the largest member. Since the
member share a common location they have the same starting address.The real purpose of unions is to
prevent memory fragmentation by arranging for a standard size for data in the memory. Java has neither
records nor unions.
The syntax of union declaration is
union union_name
{
type element 1;
type element 2;
……………..
type element n;
};
This declares a type template. Variables are then declared as:
union union_name x,y,z;
For example, the following code declares a union data type called Student and a union variable called stud:
union student
{
int rollno;
float totalmark;
};
Pointer
Pointer is a variable that represents the location of a data item, such as variable or an array element. Within
the o puter’s e or , e er stored data ite o upies o e or ore o tiguous e or ells. The u er
of memory cells required to store a data item depends on the type of the data item. For example, a single
character will typically be stored in one byte of memory; an integer usually requires two contiguous bytes, a
floating-point number usually requires four contiguous bytes, and a double precision usually requires eight
contiguous bytes.
For example, a C program contains the following declarations.
int i,*ptri;
float f,*ptrf;
The first line declares i to be an integer type variable and ptri to be a pointer variable whose object is an
integer quantity. The second line declares f to be a floating-point type variable and ptrf to be a pointer
variable whose object is a floating point quantity.
Uses
i. Addressing flexibility (support indirect addressing)
ii. Dynamic storage management (scoping)
Design Issues
What is the scope and lifetime of pointer variables?
i. What is the lifetime of heap-dynamic variables?
ii. Are pointers restricted to pointing at a particular type?
iii. Are pointers used for dynamic storage management, indirect addressing, or both?
iv. Should a language support pointer types, reference types, or both?
Variables
A variable is an abstraction of a memory cell. They have the following characteristics:
i. Name The identifier that refers to the variable. Variables created dynamically with new or malloc can be
anonymous.
ii. Address The location in memory where the variable is stored. A single variable can have multiple different
addresses as a program runs. It is also possible that multiple variables can refer to the same address. An
address is so eti es alled a L-Value si e it is eeded for the left-hand side of an assignment.
iii. Value The o te ts of the aria le. A alue is so eti es alled a R-Value si e it is eeded for the right-
hand side of an assignment.
iv. Type Determines what possible values can be stored in the variable and what operations are permitted on
it. We will discuss types more in the future.
v. Lifetime The period of time when a variable exists.
vi Scope The portion of code which can access the variable.
int main() {
int a;
int b;
}
The above program creates two variables to reserve two memory locations with names a and b. We created
these variables using int keyword to specify variable data type which means we want to store integer values in
these two variables. Similarly, you can create variables to store long, float, char or any other data type
Binding
A binding is an association between an entity and an attribute, such as between a variable and its type or
value, or between a function and its code.
Binding time is the point at which a binding takes place. There are different times a binding can happen:
i. Design Time Some binding decisions are made when a language is designed such as the binding of + to
addition in C, the operations of the String class in Java and so on.
ii. Compile Time Bindings can also be done while the program is compiled such as binding variables to types in
C++ or Java.
iii. Link Time aaasLink time is when compiled code is combined into a full program for C and C++. At this time,
global and static variables are bound to addresses.
iv. Run Time Many bindings happen at run time including most values being bound to variables. In dynamically
typed languages like Python, types are bound at run time as well.
Any binding that happens at run time is called dynamic, and any that happens before run time is called static.
Things that are statically bound can not change as a program runs, and those that are dynamically bound can
change.
What bindings take place in the following C code?
count = count + 5;
The type of count is bound at compile time
The set of possible values of count is bound at design time
The meaning of the operator symbol + is bound at compile time, when the types of its operands have
been determined.
The internal representation of the literal 5 is bound at design time.
The value of count is bound at execution time with this statement
print(total)
This ode pri ts 0 i stead of 5050". This t pe of error a e diffi ult to de ug.
TYPE CHECKING
Type checking is the process of verifying and enforcing the constraints of types, and it can occur either at
compile time (i.e. statically) or at runtime (i.e. dynamically). Type checking is all about ensuring that the
program is type-safe, meaning that the possibility of type errors is kept to a minimum. A type error is an
erroneous program behavior in which an operation occurs (or trys to occur) on a particular data type that it’s
not meant to occur on. This could be a situation where an operation is performed on an integer with the
intent that it is a float, or even something such as adding a string and an integer together:
Strong Typing
A strongly typed language is one in which each name in a program in the language has a single type associated
with it, and that type is known as compile time. The essence of this definition is that all types are statically
bound. The weakness of this definition is that it ignores the possibility that the storage location to which it is
bound may store values of different types at different times. We define a programming language to
be strongly typed if type errors are always detected.
Type Compatibility
There are two types of compatibility methods: name compatibility and structure compatibility.
Name type compatibility means that 2 variables have compatible types only if they are in either the same
declaration or in declarations that use the same type name.
Example: int x; int y; // x and y are name type compatible (if the variables are declared using the same
declaration or the same type)
Structure type compatibility means that 2 variables have compatible types if either type has identical
structures.
Example struct foo { int x; float y; };
struct bar { int x; float y;};
foo a; bar b; // a and b are structure type compatible (if the variables have the same structure even
though they are of differently named type)
Named constants- a constant is a value that cannot be altered by the program during normal execution, i.e.,
the value is constant. When associated with an identifier, a constant is said to be "named," although the terms
"constant" and "named constant" are often used interchangeably. This is contrasted with a variable, which is
an identifier with a value that can be changed during normal execution, i.e., the value is variable. Constants
are useful for both programmers and compilers: for programmers they are a form of self-documenting code
and allow reasoning about correctness; while for compilers they allow compile-time and run-time checks that
constancy assumptions are not violated, and allow or simplify some compiler optimizations.
Variable initialization- A variable provides us with named storage that our programs can manipulate. Each
variable in Java has a specific type, which determines the size and layout of the variable's memory; the range
of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Initialization is often done on the declaration statement, e.g., in Java
int sum = 0;
There are three ki ds of aria les i Ja a −
(a) Local variables
(b) Instance variables
(c) Class/Static variables
Local Variables
1. Local variables are declared in methods, constructors, or blocks.
2. Local variables are created when the method, constructor or block is entered and the variable will be
destroyed once it exits the method, constructor, or block.
3. Access modifiers cannot be used for local variables.
4. Local variables are visible only within the declared method, constructor, or block.
5. Local variables are implemented at stack level internally.
Instance Variables
1. Instance variables are declared in a class, but outside a method, constructor or any block.
2. When a space is allocated for an object in the heap, a slot for each instance variable value is created.
3. Instance variables are created when an object is created with the use of the keyword 'new' and
destroyed when the object is destroyed.
4. Instance variables hold values that must be referenced by more than one method, constructor or
block, or essential parts of an object's state that must be present throughout the class.
Class/Static Variables
1. Class variables also known as static variables are declared with the static keyword in a class, but
outside a method, constructor or a block.
2. There would only be one copy of each class variable per class, regardless of how many objects are
created from it.
3. Static variables are rarely used other than being declared as constants. Constants are variables that are
declared as public/private, final, and static. Constant variables never change from their initial value.
4. Static variables are stored in the static memory. It is rare to use static variables other than declared
final and used as either public or private constants.
Sequence control with Expressions: -The control of the order of execution of the operations both primitive
and user defined.
Implicit: determined by the order of the statements in the source program
or by the built-in execution model
Explicit: the programmer uses statements to change the order of execution
(e.g. uses If statement)
Conditional Statements-
In the programs that we have examined to this point, each of the statements is executed once, in the order
given. Most programs are more complicated because the sequence of statements and the number of times
each is executed can vary. We use the term control flow to refer to statement sequencing in a program.
If Statement
The simplest if structure involves a single executable statement. Execution of the statement occurs only if the
condition is true.
Syntax:
if (condition)
statement;
If-else statement
In if-else statement if the condition is true, then the true statement(s), immediately following the if-statement
are executed otherwise the false statement(s) are executed. The use of else basically allows an alternative set
of statements to be executed if the condition is false.
Syntax:
If (condition)
{
Statement(s);
}
else
{
statement(s);
}
IF -else if statement
It can be used to choose one block of statements from many blocks of statements. The condition which is true
only its block of statements is executed and remaining are skipped.
Syntax:
if (condition)
{
statement(s);
}
else if (condition)
{
statement(s);
}
else
{
(statement);
}
Switch Statement
Switch statement is alternative of nested if-else.it is executed when there are many choices and only one is to
be executed.
Syntax:
switch(expression)
{
case 1:
statement;
break;
case 2:
statement;
break;
.
.
.
.
case N:
statement;
break;
default:
statement;
}
Loops
Looping statement are the statements execute one or more statement repeatedly several number of times. In
C programming language there are three types of loops; while, for and do-while.
Advantage with looping statement
i. Reduce length of Code
ii. Take less memory space.
iii. Burden on the developer is reducing.
iv. Time consuming process to execute the program is reduced.
Types of Loops.
There are three types of Loops available in 'C' programming language.
while loop-
Syntax-
While(expression)
{ Block of statements; }
for loop
Syntax
for ( expression1; expression2; expression3)
{
Single statement
or
Block of statements;
}
do..while
Syntax-
do
{
Single statement
or
Block of statements;
}while(expression);
Exception handling-
An exception is a problem that arises during the execution of a program. A C++ exception is a response to an
exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is
built upon three keywords: try, catch, and throw.
Throw − A progra thro s a e eptio he a pro le sho s up. This is do e usi g a throw keyword.
Catch − A progra at hes a e eptio ith a e eptio ha dler at the pla e i a progra here ou a t
to handle the problem. The catch keyword indicates the catching of an exception.
Try − A try block identifies a block of code for which particular exceptions will be activated. It's followed by
one or more catch blocks.