PPL Unit - II
PPL Unit - II
Unit - II
Names, Bindings, Scopes, Data types, Expressions,
Statements, and Control Structures
Names,
Bindings,
Scopes,
Data
types,
Expression
s,
What you will
Learn
➔ Fundamental semantic issues of
variables
➔ Describing the nature of names and
special words in programming
languages
➔ The attributes of variables, including
type, address, and value
What you will
Learn(Contd…)
➔ Important concepts of binding and binding times
➔ The different possible binding times for variable
attributes
and how they define four different categories of
variables
➔ Two very different scoping rules for names, static
and dynamic
➔ The concept of a referencing environment of a
statement
What do you mean by
Functional & Imperative
Programming
Example for Imperative Programming
● Start
● Turn on your shoes size 9 1/2.
● Make room in your pocket to keep an array[7]
of keys.
● Put the keys in the room for the keys in the
pocket.
● Enter garage.
● Open garage.
● Enter Car.
Among the most important of these issues are the scope and
lifetime of variables
Names, Bindings, and Scopes
Functional programming languages allow expressions to be named.
Pure functional languages do not have variables that are like those of the
imperative languages. However, many functional languages do include
such variables.
Design Issues
Names in Java, C#, and Ada have no length limit, and all characters in
them are significant.
For example, the following three names are distinct in C++: rose,
ROSE, and Rose.
Name Forms
In Java and C#, the problem cannot be escaped because many of the
predefined names include both uppercase and lowercase letters.
Integer Apple
Integer = 4
Special Words
A reserved word is a special word of a programming language that
cannot be used as a name.
Integer Real
Real Integer
Special Words
There is one potential problem with reserved words: If the language
includes a large number of reserved words, the user may have difficulty
making up names that are not reserved.
The best example of this is COBOL, which has 300 reserved words.
Some of the original justifications for aliases are no longer valid; e.g.,
memory reuse in FORTRAN
Type - determines the range of values of variables and the set of operations
that are defined for values of that type; in the case of floating point,
type also determines the Precision
Variables
Value - the contents of the location with which the variable is
associated
Abstract memory cell - the physical cell or collection of cells
associated with a variable
The address of a variable is sometimes called its
l-value, A variable’s value is sometimes called its
r-value
Aliases - Example if variables named total and sum are aliases, any
change to the value of total also changes the value of sum and vice
versa. A reader of the program must always remember that total and
sum are different names for the same memory cell.
Binding
var sum = 0;
var total = 0.0;
var name =
"Fred";
The types of
sum, total,
and name are
int, float, and
string,
respectively.
Dynamic Type Binding
static,
stack-dynamic,
explicit heap-dynamic, and
implicit heap-dynamic.
Scope of the Variables
The nonlocal variables of a program unit or block are those that are
visible within the program unit or block but are not declared there.
The lifetime of that variable is the period of time beginning when the
method is entered and ending when execution of the method
terminates.
Integer
Floating Point
Complex
Decimal
Boolean Types
Character
Types
Primitive Data Types - Integer
Simplest of all
Range of values: two elements, one for true and one for
false
Could be implemented as bits, but often as bytes
Boolean types are often used to represent switches or
flags in programs.
Advantage: readability
Primitive Data Types - Character
Source:
Character String Type in Certain
Languages
C & C++ Not primitive, Use char arrays and a library of
functions that provide operations
SNOBOL4 (a string manipulation language) Primitive,
Many operations, including elaborate pattern
matching
Fortran and Python Primitive type with assignment and
several operations
Java Primitive via the String class
Perl, JavaScript, Ruby, and PHP Provide built-in
pattern matching, using regular expressions
User-Defined Ordinal
Types
An ordinal type is one in which the range of
possible values can be easily associated with
the set of positive integers.
In Java, for example, the primitive ordinal types
are
integer, char, and boolean.
There are two user-defined ordinal types that
have been supported by programming
languages: enumeration and subrange.
Enumeration
Type
An enumeration type is one in which all of the
possible values, which are named constants, are
provided, or enumerated, in the definition.
Enumeration types provide a way of defining and
grouping collections of named constants, which are
called enumeration constants.
The definition of a typical enumeration type is
shown in the following C# example:
enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
Source:
https://www.w3resource.com/c-
Subrange
Types
A subrange type is a contiguous subsequence of
an ordinal type.
For example, 12..14 is a subrange of integer
type.
Subrange types were introduced by Pascal and are
included in Ada.
There are no design issues that are specific to
subrange types.
Array
Types
An array is an aggregate of homogeneous data elements
in which an individual element is identified by its
position in the aggregate, relative to the first element
In many languages, such as C, C++, Java, Ada, and C#,
all of the elements of an array are required to be of the
same type.
In these languages, pointers and references are
restricted to point to or reference a single type.
Array Design
Issues
What types are legal for subscripts?
Are subscripting expressions in element references
range checked?
expression A × B + C
which is
equivalent to if
($flag) {
$count1 = 0;
} else {
$count2 = 0;
Compound Assignment Operators