0% found this document useful (0 votes)
98 views

PPL Unit-II - Datatypes Final

The document discusses various data types including primitive, user-defined, array, associative, record, union, pointer, and reference types. It also covers expressions, statements, and control structures like arithmetic, relational, boolean expressions, mixed-mode assignment, selection, iteration, and unconditional statements.

Uploaded by

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

PPL Unit-II - Datatypes Final

The document discusses various data types including primitive, user-defined, array, associative, record, union, pointer, and reference types. It also covers expressions, statements, and control structures like arithmetic, relational, boolean expressions, mixed-mode assignment, selection, iteration, and unconditional statements.

Uploaded by

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

Unit-II

Data types: Introduction, Primitive,Character, User-


Defined,Array,Associative,Record,Union,Pointer and Reference types, design and
implementation uses related to these types. Names, Variables, Concept of Binding,
Type Checking, Strong typing, Type Compatibility ,Named Constants, variable
initialization.

Expressions And Statements & Control Structures : Arithmetic, Relational and


Boolean Expressions, Short-Circuit Evaluation , Mixed-Mode Assignment,
Assignment Statements, Control Structures-Statement Level , Compound
Statements Selection ,Iteration, Unconditional Statements, Guarded Commands.
Introduction
• What do you mean when you declare
int n;
– Possible values of n?
– Possible operations on n?
• How about freshman defined below?
typedef struct {
char name[30];
int student_number;
} Student;
Student freshman = {“John", 644101};
3
Evolution of Data Types
The concepts of data types have evolved since the last
60 years.
For ex: In the pre 90s Fortran language used linked
lists and binary trees were implemented with arrays.
The data structures of COBOL used decimal data type
first and also provided structured data type for
records of information.

4
Data Type
• A data type is a set
– When you declare that a variable has a certain type, you are
saying that
(1) the values that the variable can have are elements of a
certain set, and
(2) there is a collection of operations that can be applied to
those values
• Fundamental issues for PL designers:
– How to define a sufficient set of data types?
– What operations are defined and how are they specified for
a data type?

5
Evolution of Data Types
• Earlier Programming language-I(PL/I) tried to include
many data types to support a wide variety of
applications.
• ALGOL 68: First programming language to
introduce a few flexible structure-defining operators
that allow a programmer to design a data structure as
per their need.
• From user-defined type to abstract data type
– The interface of a type (visible to user) is separated
from the representation and set of operations on values
of that type (invisible to user)

6
A data type defines a collection of data objects and a set of
predefined operations on those objects
A descriptor is the collection of the attributes of a variable

An object represents an instance of a user-defined (abstract


data) type
One design issue for all data types: What operations are
defined and how are they specified?

7
Uses for Types
• Program organization and documentation
– Separate types for separate concepts
• Represent concepts from problem domain
– Indicate intended use of declared identifiers
• Types can be checked, unlike program comments

• Identify and prevent errors


– Compile-time or run-time checking can prevent
meaningless computations, e.g., 3+TRUE–“Bill”
• Support optimization
– Example: short integers require fewer bits
– Access record component by known offset
8
Overview
• Various Data Types
– Primitive Data Types, Character String Types, User-
Defined Ordinal Types, Array Types, Associative Arrays,
Record Types, Union Types, Pointer and Reference
Types
• Type Binding
• Type Checking
• Strong Typing
• Type Equivalence
• Theory and Data Types
9
Primitive Data Types
Almost all programming languages provide a set of
primitive data types
Those not defined in terms of other data types
Integer, floating-point, Boolean, character
Some primitive data types are merely reflections of
the hardware
Others require only a little non-hardware support for
their implementation

10
Integer
• It’s the most common primitive data type. All
programming languages have support for integers. Ex:
Java supports byte, short, int and long.
• A signed integer value is represented in a computer
by a string of bits, with one of the bits representing
the sign.
• Most integer types are supported by the hardware
except long int of python.

11
Floating Point
They model real numbers only an approximation due to round-
off error
The float type is the standard size, usually being stored in four
bytes of memory.
The double type is provided for situations where larger
fractional parts and/or a larger range of exponents is needed

12
• Floating-point values are represented as fractions and
exponents, a form that is borrowed from scientific notation
• The collection of values that can be represented by a
floating-point type is defined in terms of precision and range
• Precision is the accuracy of the fractional part of a value,
measured as the number of bits
• Range is a combination of the range of fractions and, more
important, the range of exponents.

13
• C, C++ and Java have two floating point types
float
double
• Most scripting languages have one floating point
type
• Python's floating point type is equivalent to a C
double
• Some scripting languages only have one kind of
number which is a floating point type.

14
IEEE Floating Point Representation
Normalize the number
one bit before decimal point
Use one bit to represent the sign (1 for negative)
Use a fixed number of bits for the exponent which is
offset to allow for negative exponents

15
16
Complex
Some programming languages support complex data
type. For ex: Fortran and Python
Complex values are represented as ordered pairs of
floating point values.
In python the imaginary part of a complex literal is
specified following it with a ‘j’ or ‘J’. Ex: (7+3j)

17
Decimal Datatype
• Most larger computers that are designed to support
business system applications have hardware support
for decimal data types.
• Decimal data types store a fixed number of decimal
digits with the implied decimal point at a fixed
position in the value.
• These are the primary data types in business
applications and so COBOL and C# have decimal
types.

18
Boolean Datatype
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.
 A Boolean value could be represented by a single bit, but
because a single bit of memory cannot be accessed
efficiently on many machines, they are often stored in the
smallest efficiently addressable cell of memory, typically a
byte.

19
Character String Types
• Values are sequences of characters
• Design issues:
– Is it a primitive type or just a special kind of array?
– Should the length of strings be static or dynamic?
– What kinds of string operations are allowed?
• Assignment and copying: what if have diff. lengths?
• Comparison (=, >, etc.)
• Catenation
• Substring reference: reference to a substring
• Pattern matching

20
Character String in C
• C and C++
– Not primitive; use char arrays, terminate with a null
character (0)
– A library of functions to provide operations instead of
primitive operators
– Problems with C string library:
• Functions in library do not guard against overflowing the
destination, e.g., strcpy(src, dest);
What if src is 50 bytes and dest is 20 bytes?
• Java
– Primitive via the String and StringBuffer class

21
Character String Length
Options
• Static: COBOL, Java’s String class
– Length is set when string is created
• Limited dynamic length: C and C++
– Length varying, up to a fixed maximum
– In C-based language, a special character is used to
indicate the end of a string’s characters, rather than
maintaining the length
• Dynamic (no maximum): SNOBOL4, Perl, JavaScript
• Ada supports all three string length options

22
Collection of
Character String Implementation variable’s attr.

Static length: compile-time descriptor


Limited dynamic length: may need a run-time
descriptor for length (but not in C and C++)
Dynamic length: need run-time descriptor;
allocation/deallocation is the biggest implementation
problem

23
User-Defined Ordinal Types
• Ordinal type: range of possible values can be easily
associated with positive integers
• Two common user-defined ordinal types
– Enumeration: All possible values, which are named
constants, are provided or enumerated in the
definition, e.g., C# example
enum days {mon, tue, wed, thu, fri, sat, sun};
– Subrange: An ordered contiguous subsequence of an
ordinal type, e.g., 12..18 is a subrange of integer type

24
Enumeration Types
• A common representation is to treat the values of an
enumeration as small integers
– May even be exposed to the programmer, as is in C:
enum coin {penny=1, nickel=5, dime=10, quarter=25};
enum escapes {BELL='\a', BACKSPACE='\b', TAB='\t',
NEWLINE='\n', VTAB='\v', RETURN='\r' };

• If integer nature of representation is exposed, may


allow some or for
Pascal:
all integer operations:
C := red to blue do P(C)
C: int x = penny + nickel + dime;

25
Evaluation of Enumerated Type
• Aid to readability, e.g., no need to code a color as a
number
• Aid to reliability, e.g., compiler can check:
– Operations (don’t allow colors to be added)
– No enumeration variable can be assigned a value
outside its defined range
– 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

26
Subrange Types
• Ada’s design
– Not new type, but rename of constrained versions
type Days is (mon, tue, wed, thu, fri,
sat, sun);
subtype Weekdays is Days range mon..fri;
subtype Index is Integer range 1..100;
Day1: Days;
Day2: Weekday;
Day2 := Day1;

27
Subrange Types
• Usually, we just use the same representation for the
subtype as for the supertype
– May be with code inserted (by the compiler) to restrict
assignments to subrange variables
• Subrange evaluation
– Aid to readability: make it clear to the readers that
variables of subrange can store only certain range of
values
– Reliability: assigning a value to a subrange variable that
is outside specified range is detected as an error

28
Array Types
• Array: an aggregate of homogeneous data elements, in
which an individual element is identified by its
position in the aggregate
– Array indexing (or subscripting): a mapping from
indices to elements
– Two types in arrays: element type, index type
• Array index types:
– FORTRAN, C: integer only
– Pascal: any ordinal type (integer, Boolean, char)
– Java: integer types only
– C, C++, Perl, and Fortran do not specify range checking,
while Java, ML, C# do

29
Categories of Arrays
• Static: subscript ranges are statically bound and
storage allocation is static (before run-time)
– Advantage: efficiency (no dynamic allocation)
– C and C++ arrays that include static modifier
• Fixed stack-dynamic: subscript ranges are statically
bound, but the allocation is done at declaration time
during execution
– Advantage: space efficiency
– C and C++ arrays without static modifier

30
Categories of Arrays (cont.)
• Stack-dynamic: subscript ranges and storage allocation
are dynamically bound at elaboration time and remain
fixed during variable lifetime
– Advantage: flexibility (the size of an array need not be
known until the array is to be used), e.g., Ada
Get(List_Len); // input array size
declare
List: array (1..List_Len) of
Integer;
begin
...
End // List array deallocated
31
Categories of Arrays (cont.)
• Fixed heap-dynamic: storage binding is dynamic but
fixed after allocation, allocated from heap
– C and C++ through malloc
• Heap-dynamic: binding of subscript ranges and
storage is dynamic and can change
– Advantage: flexibility (arrays can grow or shrink during
execution), e.g., Perl, JavaScript
– C#: through ArrayList; objects created without
element and added later with Add
ArrayList intList = new ArrayList();
intList.Add(nextOne);

32
Array Initialization and Operations
Some languages allow initialization at the time of storage
allocation
 C, C++, Java, C# example
int list [] = {4, 5, 7, 83}
 Java initialization of String objects
String[] names = {“Bob”, “Jake”, “Joe”};
Ada allows array assignment and catenation
Fortran provides elemental operations, e.g.,
 + operator between two arrays results in an array of the sums
of the element pairs of the two arrays

33
Associative Arrays
• An unordered collection of data elements indexed by
an equal number of values (keys)
– User defined keys must be stored
• Perl:
%hi_temps = ("Mon" => 77, "Tue" => 79, “Wed”
=> 65, …);
$hi_temps{"Wed"} = 83;
• $ begins the name of a scalar variable

delete $hi_temps{"Tue"};
• Elements can be removed with delete

34
Rectangular and Jagged Arrays
• A rectangular array is a multi dimensioned array in
which all of the rows have the same number of elements
and all of the columns have the same number of elements.
Rectangular arrays model rectangular tables exactly.
• 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. So, if there is a third dimension (layers), each
layer can have a different number of elements. Jagged
arrays are made possible when multi dimensioned arrays
are actually arrays of arrays. For example, a matrix would
appear as an array of single-dimensioned arrays.
35
Slices

• A slice of an array is some substructure of that array.


• For example, if A is a matrix, then the first row of A is
one possible slice, as are the last row and the first
column. It is important to realize that a slice is not a
new data type. Rather ,it is a mechanism for
referencing part of an array as a unit.

36
Array Implementation
• Implementing arrays requires considerably more compile-time effort than does
implementing primitive types. The code to allow accessing of array elements must be
generated at compile time. At run time, this code must be executed to produce element
addresses. There is no way to pre compute the address to be accessed by a reference such
as list [k].
• A single-dimensioned array is implemented as a list of adjacent memory cells. Suppose
the array list is defined to have a subscript range lower bound of 0. The access function
for list is often of the form address ( list [k] ) = address (list [0] ) + k * element_size
where the first operand of the addition is the constant part of the access function, and the
second is the variable part .

37
Array Implementation

38
Multi Dimensional Arrays-A
Representation

39
Record Datatypes
• A Record is an aggregate of data elements in which
the individual elements are identified by names and
individual elements are not of the same data type or
size.
• The fundamental difference between records and
arrays is that record elements are not referenced by
indices. Instead the fields are named with identifiers
and references to the fields are made using these
identifiers.

40
Record Types
• A record is a possibly heterogeneous aggregate of data
elements in which the individual elements are
identified by names
• COBOL uses level numbers to show nested records
(others use recursive definition)
01 EMP-REC.
02 EMP-NAME.
05 FIRST PIC X(20).
05 MID PIC X(10).
05 LAST PIC X(20).
02 HOURLY-RATE PIC 99V99.

41
References and Operations
1. COBOL
– field_name OF record_name_1 OF ... OF
record_name_n
2. Others (dot notation)
– record_name_1.record_name_2. ...
record_name_n.field_name
• Operations:
– Assignment is common if types are identical
– Ada allows record comparison
– COBOL provides MOVE CORRESPONDING
• Copies a field of the source record to the corresponding field
in the target record

42
Records-Evaluation

43
Unions Types
• A union is a type whose variables are allowed to store
different types of values at different times
union time {
long simpleDate;
double perciseDate;} mytime;
...
printTime(mytime.perciseDate);
• Design issues
– Should type checking be required?
– Should unions be embedded in records?

44
Discriminated vs. Free Unions
• In Fortran, C, and C++, no language-supported type
checking for union, called free union
• Most common way of remembering what is in a union
is to embed it in a structure
struct var_type {
int type_in_union;
union {
float un_float;
int un_int; } vt_un;
} var_type;
45
Discriminated vs. Free Unions
Discriminated union: in order to type-checking
unions, each union includes a type indicator called a
discriminant
Supported by Ada
Can be changed only by assigning entire record,
including discriminant  no inconsistent records

46
Ada Union Types
type Shape is (Circle, Triangle, Rectangle);
type Colors is (Red, Green, Blue);
type Figure (Form: Shape) is record
Filled: Boolean;
Color: Colors;
case Form is
when Circle => Diameter: Float;
when Triangle =>
Leftside, Rightside: Integer;
Angle: Float;
when Rectangle => Side1,
Figure_1 Side2:
:= (Filled => Integer;
True,
end case; Color => Blue, Form => Rectangle,
end record; Side_1 => 2, Sice_2 => 3);
47
Ada Union Types
A discriminated union of three shape variables

48
Evaluation of Unions
Free unions are unsafe
Do not allow type checking
Java and C# do not support unions
Reflective of growing concerns for safety in
programming language
Ada’s descriminated unions are safe

49
Pointer and Reference Types
A pointer type variable has a range of values that
consists of memory addresses and a special value, nil
Provide the power of indirect addressing
Provide a way to manage dynamic memory
A pointer can be used to access a location in the area
where storage is dynamically created (heap)

50
Design Issues of Pointers
• What are the scope of and lifetime of a pointer
variable?
• What is the lifetime of a heap-dynamic variable?
• Are pointers restricted as to the type of value to which
they can point?
• Are pointers used for dynamic storage management,
indirect addressing, or both?
• Should the language support pointer types, reference
types, or both?

51
Pointer Operations
• Two fundamental operations: assignment and
dereferencing
• Assignment sets a pointer variable’s value to some
useful address
• Dereferencing yields the value stored at the location
represented by the pointer’s value
– Dereferencing can be explicit or implicit
– C and C++ uses an explicit operator *
j = *ptr
sets j to the value located at ptr

52
Pointer Assignment
The assignment operation j = *ptr

53
Problems with Pointers
• Dangling pointers (dangerous)
– A pointer points to a heap-dynamic variable that has
been deallocated
 has pointer but no storage
– What happen when deferencing a dangling pointer?
• Lost heap-dynamic variable
– An allocated heap-dynamic variable that is no longer
accessible to the user program (often called garbage)
 has storage but no pointer
• The process of losing heap-dynamic variables is called
memory leakage

54
Pointers in C and C++
• Extremely flexible but must be used with care
• Pointers can point at any variable regardless of when
it was allocated
• Used for dynamic storage management and
addressing
• Pointer arithmetic is possible
• Explicit dereferencing and address-of operators
• Domain type need not be fixed (void *)
– void * can point to any type and can be type checked
(cannot be de-referenced)
55
Pointer Arithmetic in C and C++
float stuff[100];
float *p;
p = stuff;

*(p+5) is equivalent to stuff[5] and p[5]


*(p+i) is equivalent to stuff[i] and p[i]

56
Reference Types
• A reference type variable refers to an object or a value in
memory, while a pointer refers to an address
 not sensible to do arithmetic on references
• C++ reference type variable: a constant pointer that is
implicitly dereferenced; primarily for formal parameters 
initialized with address at definition, remain constant
int result = 0;
int &ref_result = result;
ref_result = 100;
• Java uses reference variables to replace pointers entirely
– Not constants, can be assigned; reference to class instances
String str1;
str1 = “This is a string.”;

57
Evaluation of Pointers
Dangling pointers and dangling objects are problems
as is heap management
Pointers are like goto's--they widen the range of cells
that can be accessed by a variable
Pointers or references are necessary for dynamic data
structures--so we can't design a language without
them

58
Dangling Pointer Problem
• Tombstone: extra heap cell that is a pointer to the
heap-dynamic variable
– The actual pointer variable points only at tombstones
– When heap-dynamic variable de-allocated, tombstone
remains but is set to nil
– Any pointer variables pointing to that heap-dynamic
variable will know it is gone by noticing tombstone
becomes nil
– Costly in time and space

59
Dangling Pointer Problem
• Locks-and-keys:
– Heap-dynamic variable represented as (variable, lock)
– Associated pointer represented as (key, address)
– When heap-dynamic variable allocated, a lock is placed
in lock cell of that variable as well as the key cell of the
corresponding pointer variable
– Any copies of the pointer value to other pointer
variables must also copy the key value
– When a heap-dynamic variable is deallocated, its lock
value is cleared to an nil
– Any remaining pointers will have a mismatch

60
Heap Management
• Two approaches to reclaim garbage
– Reference counters (eager): reclamation is gradual
– Garbage collection (lazy): reclamation occurs when the
list of variable space becomes empty
• Reference counters:
– A counter in every variable, storing number of pointers
currently pointing at that variable
– If counter becomes zero, variable becomes garbage and
can be reclaimed
– Disadvantages: space required, execution time required,
complications for cells connected circularly

61
Garbage Collection
• Run-time system allocates cells as requested and
disconnects pointers from cells as needed. Garbage
collection when out of space
– Every heap cell has a bit used by collection algorithm
– All cells initially set to garbage
– All pointers traced into heap, and reachable cells
marked as not garbage
– All garbage cells returned to list of available cells
– Disadvantages: when you need it most, it works worst
(takes most time when program needs most of cells in
heap)

62
Type Checking
The activity of ensuring that the operands of
an operator are of compatible types
A compatible type is one that is either legal for the
operator, or is allowed to be implicitly converted,
by compiler-generated code, to a legal type, e.g.,
(int) A =(int) B + (real) C
This automatic conversion is called a coercion
All type bindings static  nearly all type
checking can be static
Type binding dynamic  type checking
dynamic
63
Type Binding
• Before a variable can be referenced in a program, it
must be bound to a data type
– How is a type specified?
– When does the binding take place?
• If static, the type may be specified by either
– Explicit declaration: by using declaration statements
– Implicit declaration: by a default mechanism, e.g., the
first appearance of the variable in the program
– Fortran, PL/I, BASIC, Perl have implicit declarations
• Advantage: writability
• Disadvantage: reliability (less trouble with Perl)

64
Dynamic Type Binding
 A variable is assigned a type when it is assigned a value in an
assignment statement and is given the type of RHS, e.g., in JavaScript
and PHP
list = [2, 4.33, 6, 8];
list = 17.3;
 Advantage: flexibility (generic for processing data of any type, esp.
any type of input data)
 Disadvantages:
 High cost (dynamic type checking and interpretation)

 Less readable, difficult to detect type error by compiler

 PL usually implemented in interpreters

65
Strong Typing
• A programming language is strongly typed if type
errors are always detected
– Advantage: allows the detection of the misuses of
variables that result in type errors
– FORTRAN 77 is not: EQUIVALENCE
– C and C++ are not: unions are not type checked
• Coercion rules can weaken strong typing
– Example: a and b are int; d is float;
no way to check a + b mistakenly typed as a + d

66
Type Equivalence
• Type checking checks compatibility of operand types
for operators  compatibility rules
• Simple and rigid for predefined scalar types
• Complex for structured types, e.g., arrays, structures,
user-defined types
– They seldom coerced  no need to check compatibility
– Important to check equivalence, i.e., compatibility
without coercion  how to define type equivalence?

67
Name Type Equivalence
• Two variables have equivalent types if they are in
either the same declaration or in declarations that use
the same type name
• Easy to implement but highly restrictive:
– Subranges of integer types are not equivalent with
integer types, e.g., Ada
type Indextype is 1..100;
count : Integer; index : Indextype;
– Formal parameters must be the same type as their
corresponding actual parameters
68
Structure Type Equivalence
• Two variables have equivalent types if their types
have identical structures
• More flexible, but harder to implement  need to
compare entire structures
– Are two record types compatible if they are structurally
the same but use different field names?
– Are two array types compatible if they are the same
except the subscripts? e.g. [1..10] and [0..9]
– Are two enumeration types compatible if their
components are spelled differently?
– How about type celsius & fahrenheit of float?

69
Type Equivalence in C
Name type equivalence for struct, enum, union
A new type for each declaration not equivalence to any
other type
Structure type equivalence for other nonscalar types,
e.g., array
typedef only defines a new name for an existing
type, not new type

70
Summary
• Data types of a language a large part determine that
language’s style and usefulness
• Primitive data types of most imperative lang. include
numeric, character, and Boolean
• The user-defined enumeration and subrange types are
convenient and add to the readability and reliability
of programs
• Arrays and records included in most languages
• Pointers are used for addressing flexibility and to
control dynamic storage management
71
Expressions And Statements &
Control Structures
– In programming languages, arithmetic expressions consist s
of operators, operands, parentheses, and function calls.

– An operator can be unary, meaning it has a single operand,
binary, meaning it has two operands, or ternary, meaning it
has three operands.

– In most programming languages, binary operators are infix,
which means they appear between their operands.

– One exception is Perl, which has some operators that are
prefix, which means they precede their operands.

72
Arithmetic Expressions
 The purpose of an arithmetic expression is to specify an
arithmetic computation
 An implementation of such a computation must cause two
actions: fetching the operands, usually from memory, and
executing arithmetic operations on those operands.
 Arithmetic expressions consist of
operators
operands
parentheses
function calls

73
Design Issues for Arithmetic
Expressions

Operator Precedence Rules

Operator Associatively Rules

Order Of Operand Evaluation

Operand Evaluation Side Effects

Operator Overloading

Mode Mixing Expressions


74
Operator Precedence
 Precedence rules define the order in which
―adjacent operators of different precedence levels
are evaluated
Typical precedence levels
parentheses
unary operators
** (if the language supports it)

75
In all of the common imperative languages the unary
minus operator appear in an expression either at the
beginning or anywhere inside the expression as long
as its parenthesized to prevent it from being next to
an operator.
for example: a+(-b)*c is legal but a+-b*c is illegal.

76
• Consider the following expressions:
-a/b
-a*b
-a ** b
• In the first two cases, the relative precedence of the unary
minus operator and the binary operator is irrelevant i.e,
the order of evaluation of two operators has no effect on
the value of the expression. But in the last case it matters.
• The ** is called as exponentiation operator. Only Fortran,
Ruby, Visual basic and Ada have this operator. In all four,
this operator has the highest precedence over unary
minus.
77
Precedences of Arithmetic
Operators of Ruby and the C Based
Languages

78
Operator Associatively Rules
 Associativity rules define the order in which adjacent operators with the
same precedence level are evaluated
 Typical associativity rules
 Left to right for arithmetic operators
 exponentiation (** or ^) is right to left
 Sometimes unary operators associate right to left (e.g., in FORTRAN)
 APL is different; all operators have equal precedence and all operators
associate right to left
 Precedence and associativity rules can be overriden with parentheses
 Exponentiation in Fortran and Ruby is right associative.
 In Visual Basic the exponentiation operator is left associative.

79
Associative Rules for common
Programming Languages

80
Paranthesis
Programmers can alter the precedence and
associativity rules by placing paranthesis in
expressions.
For ex: In the expression, (A+B)*C the addition will be
performed first although multiplication has
precedence over addition, because of the paranthesis.

81
Expressions in Ruby
Ruby is a pure object oriented language.
All the operators in Ruby are implemented as
methods.
For ex: the expression a+b is a call to the + method of
the object referenced by ‘a’ passing the object
referenced by ‘b’ as a parameter.

82
Expressions in LISP
All arithmetic and logic operations in Lisp are
performed by subprograms, but they have to be
explicitly called.
For ex, to specify the c expression a+b*c in lisp, we
should write (+a (*b c))
In the above expression + and * are the names of
functions.

83
Conditional Expressions
If-then-else statements can be used to perform a
conditional expression assignment. For ex, consider the
statement
if(count==0)
avg=0;
else
avg=sum/count;
In C based languages, this code can be specified more
conveniently in an assignment statement using a
conditional expression, which has the following form:
expression_1?expression_2:expression_3

84
Operand Evaluation order
Variables in expressions are evaluated by fetching
their values from memory.
Sometimes constants are also evaluated in the same
way.
In other cases a constant may be a part of the
machine language instruction and so a memory fetch
is not needed.
If an operand is a paranthesized expression all of the
operators it contains must be evaluated before its
value can be used as an operand.
85
Operand Evaluation
order(contd…)
If neither of the operands of an operator has side
effects , then operand evaluation order is irrelevant.
Therefore the only interesting case arises when the
evaluation of an operand has side-effects.
What is a side-effect?
A side-effect is a function(functional side-effect) occurs
when the function changes either one of its
parameters or a global variable.

86
Operand Evaluation
order(contd…)
Ex: consider the following expression
a+fun(a)
If fun does not have the side effect of changing ‘a’,
then the order of evaluation of the two operands a
and fun(a) has no effect on the value of the
expression. However if fun changes a, there is a side-
effect.

87
Operand Evaluation
order(contd…)
Consider the following situation:
If fun returns 10 and changes the value of its parameter to 20.
Suppose we have
a=10;
b=a+fun(a);
Then if the value of a is fetched first(in the expression
evaluation process), its value is 10 and value of expression
is 20.
But if the second operand is evaluated first then the value of
the first operand is 20 and the value of the expression is 30.

88
Overloaded Operators
Arithmetic operators are often used for more than
one purpose.
For ex, + is usually used to specify integer,float
additions.
Thus multiple use of an operator is called Operator
Overloading.

89
As an example of possible dangers of overloading,
consider the use of ‘&’ in c++.
-As a binary operator, it specifies a bitwise logical AND
operation. &&
-As a unary operator, its meaning is totally different. &
-As a unary operator with a variable as its operand, the
expression value is the address of that variable. In this
case the ampersand is called the address of operator.
For ex: x=&y causes the address of y to be placed in x.

90
There are two problems with this multiple use of the
ampersand.
First, using the same symbol for two completely
unrelated operations eliminates the readability.
Second, the simple keying error of leaving out the
first operand for a bitwise AND operation can go
undetected by the compiler because it is interpreted
as an address-of operator.

91
Mode mixing Conversions/Type
Conversions
Type conversions are either narrowing or widening.
A narrow conversion converts a value to a type that
cannot store even approximations of all of the values
of the original type.
For ex, converting a double to a float in Java is a
narrowing conversion because the range of double is
much larger than that of a float.

92
A widening conversion converts a value to a type that can
include atleast approximations of all the values of the
original type.
For ex,converting an int to a float in Java is a widening
converison.
-Widening conversion is always safe because the
approximate magnitude of the converted value is
maintained.
-Narrowing conversions are not always safe as the
magnitude of the converted value is changed in the
process.
93
Relational and Boolean
Expressions
A relational operator is an operator that compares the
values of its two operands.
A relational expression is boolean , except when
boolean is not included in the language.
The syntax of the relational operators for equality and
inequality differs among some programming
languages.
For ex, for inequality c language uses !=, Fortran
uses .NE.

94
Javascript and PHP have two additional relational
operators, === and !==.
These are similar to == and != but prevent their
operands from being coerced.
For ex: the expression “7”==7 is true in JavaScript
because when a string and a number are the operands
of a relational operator, the string is coerced to a
number.

95
However “7==7” is false because no coercion is done on
the operands of this operator.
Ruby uses “==“ for the equality relational operator that
uses coercions and “eq?” for equality with no coercions.
The relational operators always have lower precedence
than the arithmetic operators, so in expressions such as
a+1>2*b
the arithmetic expressions are evaluated first.

96
Boolean Expressions
Boolean expressions consists of Boolean variables,
Boolean constants, relational expressions and Boolean
operators.
The operators usually include those for the AND,OR
and NOT operations and sometimes for exclusive OR
and equivalence.
Boolean operators usually take only Boolean
operands and produce Boolean values.

97
Boolean Expressions(contd..)
In the mathematics of Boolean algebras, the OR and
AND operators must have equal precedence.
The C based language assign a higher precedence to
AND than OR.
Because the arithmetic expressions can be the
operands of relatonal expressions, and relational
expressions can be the operands of Boolean
expressions, the three categories of operators must be
placed in different precedence levels, related to each
other.
98
Precedence of Arithmetic, Relational
and Boolean Operators in C-based
languages is as follows

99
Boolean Expressions(contd..)
One odd result of C’s design is that the following
expression is legal.
a>b>c
In the above expression the leftmost relational operator
is evaluated first because the relational operators of C
are left associative, producing either 1 or 0.Then this
result is compared with the variable c. There is no
comparison of b and c in this expression.

100
Boolean Expressions(contd..)
Some languages including Perl and Ruby provide two
sets of the binary logic operators && ,and for AND
and | |, or for OR.
One difference between && ,and and ||,or is that the
spelled versions have lower precedence.
Also and,or have equal precedence but && has
higher precedence than||.

101
Short Circuit Evaluation
A short circuit evaluation of an expression is one in which the
result is determined without evaluating all the operands or
operators.
For ex: (13*a)*(b/13-1) is independent of the value of (b/13-1) if a is
0.Hence when a is 0, there is no need of evaluating the whole
expression. However in arithmetic expressions, this shortcut is not
easily detected during execution.
The value if Boolean expression (a>=0) && (b<10) is independent
of the second relational expression if a<0, because the
expression(FALSE&&(b<10)) is false for all values of b. So when
a<0 there is no need to evaluate the entire expression

102
Short Circuit
Evaluation(contd..)
In C based languages , the AND and OR operators(&&
and ||) are short-circuit. But bitwise operators(& and
|) are not short-circuit.
All the logical operators of Ruby,Perl,ML,F# and
Python are short-circuit evaluated.

103
Assignment Statements
Assignment statement is one of the central constructs
in imperative languages. It provides the mechanism
by which the user can dynamically change the
bindings of the values to variables.
Nearly all the programming languages use the equal
sign for assignment operator.
It is important to use a different equal sign for the
equality relational operator to avoid confusion.

104
Algol 60 pioneered the use of := as the assignment
operator which avoids the confusion of assignment
with equality.
Ada also uses this assignment operator.
In some languages such as Fortran and Ada, an
assignment can appear as a stand alone statement,
and the designation is restricted to a single variable.

105
Perl allows conditional targets on assignment
statements.
For ex: ($flag? $count1: $count2)=0; is equivalent to
if($flag)
{
$count1=0;
}
else
{
$count2=0;
}

106
Perl allows conditional targets on assignment
statements.
for ex: consider($flag ? count1: $count2)=0;
Which is equivalent to
if( $flag) {
$count1=0;
}
else
{
$count2=0;
}
107
Compound Assignment
Statements
It is a shorthand method of specifying a commonly
needed form of an assignment.
Compound assignment operators were introduced by
ALGOL 68, and were adopted by C and other C based
languages as well as Perl, JavaScript, Python and Ruby
For ex: sum+=value; is equivalent to
sum=sum+value;

108
Unary Assignment Operators
The c-based languages, Perl, and JavaScript include
two special unary arithmetic operators that are
actually abbreviated assignments.
They combine increment and decrement operations
with assignment.
The operators increment ++ and decrement – can be
used either in expressions or to form stand-alone
single operator assignment statements.
They can appear either as a prefix operators or as
postfix operators.
109
For ex in the assignment statement sum=++count;
the value of count is incremented by 1 and then
assigned to sum. This operation can also be stated as
count=count+1;
sum=count;
If the same operator is used as a postfix operator as in
sum=count++;
the assignment of the value of count to sum occurs first
then count is incremented.

110
The following statement count++; increments
count.It is also treated as an assignment statement
only.
When two unary operators apply to the same
operand, the association is right to left. For ex in
-count++;
count is first incremented and then negated. So its
equivalent to –(count++) rather than (-count)++

111
In the C based languages, Perl and JavaScript, the
assignment statement produces a result, which is the
same as the value assigned to the target.
It can be used as an expression and as an operand in
other expressions.
For ex: while((ch=getchar())!=EOF)
{…
}
The above statement is common and valid in C.

112
Multiple Assignment
Statements
Several programming languages including Perl, Ruby and
Lua, provide multiple-target, multiple- source assignment
statements.
For ex: ($first, $second, $third)={20,30,40};
The semantics is that $first is assigned to 20, $ second is
assigned the value 30 and $third is assigned the value of
40.

113
Assignment in Functional
Programming Language
All of the identifiers used in pure functional
languages and some of them used in other functional
languages are just names of values. As such their
values never change.
For example in ML (meta language), names are
bound to values with the val declaration :
 Val cost=quantity*price;
**If cost appears on the left side of a subsequent val
declaration that declaration creates a new version of the
name cost, which has no relationship with the previous
version.

114
Mixed Mode Assignment
C, C++ and Perl use coercion rules for mixed-mode
assignment that are similar to those they use for
mixed-mode expressions i.e, many of the possible
type mixes are legal, with coercion freely applied.
Java and C# allow mixed-mode assignment only if the
required coercion is widening. So an int value can be
assigned to a float variable but not vice-versa.
Disallowing half of the possible mixed-mode
assignments is a simple but effective way to increase
the reliability of Java and C# relative to C and C++.
115
Control Structures
A control structure is a control statement and
collection of statements whose execution it controls.
One design issue that is relevant to all of the selection
and iteration control statements: Should the control
structure have multiple entries?
All selection and iteration constructs control the
execution of code segments and the question is
whether the execution of those code segments always
begins with the first statement in the segment.

116
Selection Statements
A selection statement provides the means of choosing
between two or more execution paths in a program.

There are 2 categories of selection statements:


a)Two way
b)n-way or multiple selection.

117
Two Way Selection Statements
Mostly all the imperative languages share the same
style and design ,only some vary.
The general form of a two way selector is as follows:
If(control-expression)
then clause
else clause

118
Design Issues
What is the form and type of the expression that
controls the selection?
– How are the then and else clauses specified?
– How should the meaning of nested selectors be
specified?

119
The Control Expression
If the “then” reserved word or some other syntactic
marker is not used to introduce the “then” clause, the
control expression is placed in parentheses.
– In C89, C99, Python, and C++, the control
expression can be arithmetic
– In languages such as Ada, Java, Ruby, and C#, the
control expression must be Boolean

120
Clause Form
In many contemporary languages, the then and else
clauses can be single statements or compound statements
– In Perl, all clauses must be delimited by braces (they
must be compound)
– In Fortran 95, Ada, and Ruby, clauses are statement
sequences
– Python uses indentation to define clauses
if x > y :
x=y
print "case 1"
121
Nesting Selectors
– Java example
if ( sum == 0)
if ( count == 0)
result = 0;
else result = 1;
Which if gets the else?
– Java's static semantics rule: else matches with the
nearest if Nesting Selectors (continued)

122
Nesting Selectors(Contd..)
To force an alternative semantics, compound
statements may be used:
if (sum == 0)
{
if (count == 0)
result = 0;
}
else result = 1;
– The above solution is used in C, C++, and C#

123
Nesting Selectors(Contd..)
Perl requires that all then and else clauses to be compound
– Statement sequences as clauses:
Ruby:
if sum == 0 then
if count == 0 then
result = 0
else
result = 1
end
end

124
Nesting Selectors(Contd..)
In the above program block, because the “end “
reserved word closes the nested if, it is clear that the
else clause is matched to the inner then clause.
The second interpretation of the selection statement
at the beginning of this section, in which the else
clause is matched to the outer if, can be written in
Ruby as follows

125
Nesting Selectors(Contd..)
If sum==0 then
If count==0 then
result=0
end
else
result=1
end
This can be written in Python in the below way:

126
Nesting Selectors(Contd..)
Python
if sum == 0 :
if count == 0 :
result = 0
else : r
esult = 1

127
Selector Expressions
In the functional languages ML, F# and LISP, the
selector is not a statement.
It is an expression that results in a value. Therefore it
can appear anywhere any other expression can appear.
Consider the example:
Let y=
If x>0 then x
else 2*x;
This creates the name y and sets it to either x or 2*x,
depending on whether x is greater than zero.
128
Multiple-Selection Statements
Allow the selection of one of any number of statements
or statement groups.
Design Issues:
– What is the form and type of the control expression?
– How are the selectable segments specified?
– Is execution flow through the structure restricted to
include just a single selectable segment?
– How are case values specified?
– What is done about unrepresented expression values?

129
Examples of Multiple-Selector
C, C++, and Java
switch (expression)
{
case
const_expr_1: stmt_1;

case const_expr_n: stmt_n;
[default: stmt_n+1]
}
130
Design choices for C_ switch
statement
– Control expression can be only an integer type
– Selectable segments can be statement sequences,
blocks, or compound statements
– Any number of segments can be executed in one
execution of the construct (there is no
implicit branch at the end of selectable segments)
– default clause is for unrepresented values (if
there is no default, the whole statement does
nothing)
131
Multiple-Way Selection: Examples
– C#
 Differs from C in that it has a static semantics rule
that disallows the implicit execution of
more than one segment
 Each selectable segment must end with an
unconditional branch (goto or break)

132
– Ada
case expression is
when choice list => stmt_sequence;
when choice list => stmt_sequence; when others =>
stmt_sequence;] end case;
– More reliable than C‗s switch (once a stmt_sequence
execution is completed, control is
passed to the first statement after the case statement

133
Multiple-Way Selection Using
if
Multiple Selectors can appear as direct extensions to
two-way selectors, using else-if clauses,
for example in Python:
if count < 10 : bag1 = True
elsif count < 100 : bag2 = True
elif count < 1000 : bag3 = True

134
Iterative Statements
The repeated execution of a statement or compound
statement is accomplished either by iteration or
recursion
–General design issues for iteration control statements:
– How is iteration controlled?
– Where is the control mechanism in the loop?
The primary possibilities for iteration control are
logical, counting or a combination of the two. The main
chances for the location of the control mechanism are
the top of the loop or the bottom of the loop.
135
The issue is not the physical placement of the control
mechanism rather it is whether the mechanism is
executed and affects control before or after execution
of the statement’s body.
A third option which allows the user to decide
whether to put the control, at the top, at the bottom
or even within the controlled segment is known as the
user located loop control mechanism.

136
Counter Controlled Loops
A counting iterative control statement has a variable
called the loop variable in which the count value is
maintained.
It also includes some means of specifying the initial
and terminal values of the loop variable and the
difference between sequential loop variable values
called as step-size.
The initial, terminal and step size specifications of a
loop are called loop parameters.

137
Design Issues
What are the type and scope of the loop variable?
Should it be legal for the loop variable or loop
parameters to be changed in the loop, and if so, does
the change affect loop control?
Should the loop parameters be evaluated only once,
or once for every iteration?

138
For statement in c based
languages
The general form of the c’s for statement is:
for(expression_1;expression_2;expression_3)
Loop body
The loop body can be a single statement, a compound
statement or a null statement
The first expression is for initialization and is evaluated
only once.
The second expression is the loop control and is
evaluated before each execution of the loop body.

139
In C, a zero value indicates false and so if the value of
the second expression is false, the loop is terminated.
The last expression in the for statement is executed
after each execution of the loop body. It is often used
to increment the loop counter.
-Design Choices for c for loop are:
There are no explicit loop variables or loop parameters.
All involved variables can be changed in the loop body.
Sometimes the expression evaluation cannot be safe
because it is legal to branch into a c for loop body.
140
Python for statement
The general form of Python for statement is:
for loop_variable in object:
-loop body
[else:
-else clause]
The loop variable is assigned the value in the object
which is a range one for each execution of the loop
body. The else clause , when present is executed if the
loop terminates normally.

141
for count in [2,4,6]:
print count
The above code produces
2
4
6
For most simple counting loops in Python, the range function is used.
Range takes one, two or three parameters.
range(5) returns [0,1,2,3,4]
Range(2,7) returns[2,3,4,5,6]

142
Design Choices for while loop:

Loop variable must be INTEGER


Loop variable always has its last value
The loop variable cannot be changed in the loop, but
the parameters can; because they are evaluated only
once, it does not affect loop control
Loop parameters are evaluated only once

143
FORTRAN 95-do while

DO label var = start, finish [, stepsize]


Stepsize can be any value but zero
Parameters can be expressions

144
User-Located Control
Mechanisms
In some situations it is convenient for a programmer
to choose a location for loop control other than top or
bottom of the loop body.
Hence some programming languages provide this
feature. A syntactic mechanism for this loop control is
relatively simple.
The design issues are: Should the control mechanism
be an integral part of the exit?
Should only one loop be exited, or can enclosing
loops be exited?
145
Unconditional Statements
C,C++ ,Python, Ruby and C# have unconditional
unlabeled exits(break statement).
Java and Perl have unconditional labeled exits(break
in Java, last in Perl).
C,C++ and Python include an unlabeled control
statement continue that transfers control to the
control mechanism of the smallest enclosing loop.
This is not not an exit but a skip to the rest of the
statements.

146
Unconditional
Statements(contd…)
C , C++, Python, Ruby, and C# have unconditional
unlabeled exits (break)
Java and Perl have unconditional labeled exits (break
in Java, last in Perl)
C, C++, and Python have an unlabeled control
statement, continue, that skips the remainder of
the current iteration, but does not exit the loop
Java and Perl have labeled versions of continue

147
Iterative Statements: Iteration
Based on Data Structures
Number of elements of in a data structure control
loop iteration
Control mechanism is a call to an iterator function
that returns the next element in some chosen order, if
there is one; else loop is terminate
C's for can be used to build a user-defined
iterator: for (p=root; p==NULL; traverse(p)){}
C#‗s foreach statement iterates on the elements
of arrays and other collections:
148
Strings[] = strList = {"Bob", "Carol", "Ted"}; foreach
(Strings name in strList)
Console.WriteLine ("Name: {0}", name);
- The notation {0} indicates the position in the string to be
displayed
– Perl has a built-in iterator for arrays and hashes, foreach
Unconditional Branching
–– C# offers goto statement (can be used in switch
statements)
– Loop exit statements are restricted and somewhat
camouflaged goto‗s
149
Transfers execution control to a specified place in the
program
– Well-known mechanism: goto statement
– Major concern: Readability
– Some languages do not support goto statement
(e.g., Java)

150
Guarded Commands
Designed by Dijkstra
Purpose: to support a new programming
methodology that supported verification
(correctness) during development
Basis for two linguistic mechanisms for concurrent
programming (in CSP and Ada)
Basic Idea: if the order of evaluation is not
important, the program should not specify one

151
Selection Guarded Command
if <Boolean exp> -> <statement> [] <Boolean exp> ->
<statement>
...
[] <Boolean exp> -> <statement> fi
when construct is reached,
–Evaluate all Boolean expressions
–If more than one are true, choose one non-
deterministically
–If none are true, it is a runtime error Selection
Guarded Command: Illustrated
152
Loop Guarded Command Form

do <Boolean> -> <statement>


[] <Boolean> -> <statement>
...
[] <Boolean> -> <statement> od
for each iteration
1. Evaluate all Boolean expressions
2. If more than one are true, choose one non-
deterministically; then start loop again
3. If none are true, exit loop

153
Features of Guarded
Commands
Connection between control statements and program
verification is intimate
Verification is impossible with goto statements
Verification is possible with only selection and logical
pretest loops
Verification is relatively simple with only guarded
commands

154

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy