Beruflich Dokumente
Kultur Dokumente
LANGUAGES
ECEg - 4182
by
Dr. T.R.SRINIVASAN, B.E., M.E., Ph.D.,
Assistant Professor,
Faculty of Electrical and Computer Engineering,
Jimma Institute of Technology,
Jimma University,
Jimma, Ethiopia
1
Data Types
Expressions and Assignment Structures
Statement-Level Control Structures
2
Data Types
3
Evolution of Data Types
FORTRAN I (1956)
- INTEGER, REAL, arrays
Ada (1983)
- User can create a unique type for every category of
variables in the problem space and have the system
enforce the types
4
Design Issues for all data types
5
Primitive Data Types
• Data Types that are not defined in terms of others types are
called Primitive Type
• Some of this types are merely reflection of hardware
• Integer
6
Floating Point
7
Decimal
• Advantage: accuracy
• They are stored very much like character strings using binary
code for decimal digits (BCD)
8
Boolean
Character Type
9
Character String Types
Design issues:
1. Is it a primitive type or just a special kind of array?
Operations:
- Assignment
- Comparison (=, >, etc.)
- Catenation
- Substring reference
- Pattern matching
10
Examples
11
String Length Options
There are several design choices regarding the length of string values
1. Static length string (length is specified at the declaration time) -
FORTRAN 90, Ada, COBOL, Pascal
• - Aid to writability
• - As a primitive type with static length, they are inexpensive
to provide
• - Dynamic length is nice, but is it worth the expense?
• Implementation:
14
Limited dynamic strings
Limited dynamic strings require a run time descriptor to store both
max length and current length
- linked list, or
- adjacent storage
15
Ordinal Types ( user defined )
Enumeration Types
- one in which the user enumerates all of the possible
values, which are symbolic constants
• Design Issue:
Should a symbolic constant be allowed to be in more
than one type definition?
16
Examples
• Pascal
cannot reuse constants; they can be used for array
subscripts, for variables, case selectors; NO input or
output; can be compared
• C and C++ -
like Pascal, except they can be input and output as
integers
17
Evaluation
Aid to readability
--e.g. no need to code a color as a number
Aid to reliability
--e.g. compiler can check operations and ranges of
values
18
Sub range Type
20
Arrays
Example: simple_array.C
#include <stream.h>
void main() {
char a[8] = "abcdefg";
cout << a[0] << *a;
cout << a[1] << *(a+1);
}
23
Subscript Types
FORTRAN, C, C++, Java - int only
C, C++, Java - 0;
FORTRAN - 1;
24
Number of subscripts
- C, C++, and Java allow just one, but elements can be arrays
- Others - no limit
Array Initialization
- Usually just a list of values that are put in the array in the
order in which the array elements are stored in memory
25
Examples
2. C and C++ - put the values in braces; can let the compiler
count them
e.g.
int stuff [] = {2, 4, 6, 8};
26
Slices
1. FORTRAN 90
INTEGER MAT (1 : 3, 1 : 3)
MAT(1 : 3, 2) - the second column
MAT(2 : 3, 1 : 3) - the second and third row
27
Implementation of Arrays
Access function maps subscript expressions to an address in the
array - Row major (by rows) or column major order (by columns)
Column major order is used in FORTRAN, but most of other languages
use row major order.
28
Associative Arrays
- Design Issues:
1. What is the form of references to elements?
2. Is the size static or dynamic?
30
Record Field References
1. COBOL
field_name OF record_name_1 OF ... OF record_name_n
31
Example
#include <stream.h>
struct student {
int ssn;
char grade;
};
void main() {
struct student john;
struct student *p_john;
john.grade = 'A';
p_john = &john;
cout << p_john grade << endl;
}
32
Record Operations
1. Assignment
- Pascal, Ada, and C allow it if the types are identical
2. Initialization
- Allowed in Ada, using an aggregate constant
3. Comparison
- In Ada, = and /=; one operand can be an aggregate constant
4. MOVE CORRESPONDING
- In COBOL - it moves all fields in the source record to fields with
the same names in the destination record
34
Unions
Examples:
1. FORTRAN - with EQUIVALENCE in C or C++ construct
union is used
(free unions)
2. Algol 68 - discriminated unions
- Use a hidden tag to maintain the current type
- Tag is implicitly set by assignment
Uses:
1. Addressing flexibility ( indirect addressing )
2. Dynamic storage management ( access to heap)
Design Issues:
1. What is the scope and lifetime of pointer variables?
2. What is the lifetime of heap-dynamic variables?
3. Are pointers restricted to pointing at a particular type?
4. Are pointers used for dynamic storage management, indirect
addressing, or both?
5. Should a language support pointer types, reference types, or
both?
36
Fundamental Pointer Operations
38
Problems with pointers
(Example 1) dangling.C
#include <stream.h>
void main() {
int *x, *y;
x = new int;
*x = 777;
delete x;
y = new int;
*y = 999;
cout << *x << endl;
}
39
Example (C++)
dangling.C
#include <stream.h>
void main() {
int *x, *y;
x = new int;
*x = 777; Example 2
delete x; int *x;
y = new int; ...
*y = 999; {
int y = 1;
cout << *x << endl; x = &y;
} }
...
cout << *x << endl;
// We don't know what's in *x
40
Lost Heap-Dynamic Variables
- Creating one:
41
Reference Types
C++ has a special kind of pointers Reference Types
which is constant pointers that are implicitly
de-referenced
float stuff[100];
float *p;
p = stuff;
42
Java
- No pointer arithmetic
43
Implementation of Pointer and Reference Types
Microcomputers
(thus are based on Intel microprocessors which uses
two part addresses: segment and offset) pointers
and references are implemented as pair of 16 bit
words, one for each part.
44
Garbage collection
45
Garbage collection
(lazy approach): Waits until all available cells have
been allocated. A garbage collection process starts
by setting indicators of all the cells to indicate
they are garbage. Then every pointer in the program
is traced, and all reachable cells are marked as
not being garbage.
46
Expressions and Assignment Statements
Arithmetic Expressions
Overloaded Operators
Type Conversations
Relational and Boolean Expressions
Short Circuit Evaluation
Assignment Statements
Mixed Mode Assignments
47
Arithmetic Expressions
- We have learned that variables are abstraction of memory cells
in a computer
48
Design issues for arithmetic expressions
49
Operators and operands
++a (unary)
those taking two operators are called binary.
a + b (binary)
A ternary operator has three operand C, C++, and Java (?:)
50
Operator Precedence
1. parentheses
2. unary operators
3. ** (if the language supports it) ( exponentiation)
4. *, /
5. +, -
51
Associatively rules
Def: The operator associatively rules for expression evaluation
define the order in which adjacent operators with the same
precedence level are evaluated
Example:
A+B–C–D=
52
Operand evaluation order
The process:
53
Operator Overloading
- Some is common (e.g., + for int and float)
Potential problems:
- Users can define nonsense operations
- Readability may suffer
54
Implicit Type Conversions
55
Relational Expressions
• Use relational operators and operands of various types
• Evaluate to some Boolean representation
• Relational operators always have lower precedence than
arithmetic Operations
a + 1 > 2*b
Boolean Expressions
Operands are Boolean and the result is Boolean
C has no Boolean type--it uses int type with for false and
nonzero for true
56
Short Circuit Evaluation
index := 1;
while (index <= length) and
(LIST[index] <> value) do
index := index + 1
57
Assignment Statements
58
More complicated assignments
1. Multiple targets A, B = 10
sum += next;
a++;
a ++ is same as (a++)
59
Assignment as an Expression
Disadvantage
- Another kind of expression side effect often leads
to expression that is hard to read and understand.
Example: a = b +( c = d/b++) – 2;
Assign b to temp
Assign b + 1 to b
Assign d/temp to c
Assign b + c to temp
Assign temp - 2 to a.
60
Mixed-Mode Assignment
61
Statement-Level Control
Structures
Compound Statements
Selection Statements
Iterative Statements
Unconditional Branching
Guarded Commands
62
Levels of Control Flow
The flow of control, or execution sequence, in program can be examined at
several levels:
1. Within expressions
63
Evolution
FORTRAN I: control statements were based directly on IBM 704 hardware
Much research and argument in the1960s about the issue
goto or not having goto
One important result: It was proven that all flowcharts can be coded with
only two-way selection and pretest logical loops
66
In ALGOL 60 it was done by using syntax; If an if must be nested in a then
clause, it must be placed in a compound statement. This means that if
statement is not allowed to be nested inside of than clause directly.
67
Multiple selection constructs
Multiple selection construct allows the selection of one or any
number of statements or statement groups switch construct. The
original form came from FORTRAN
68
Early Multiple Selectors
Bad aspects:
- Not encapsulated (selectable segments could
be anywhere)
- Segments require GOTOs
69
Modern Multiple Selectors
Pascal case
case expression of
constant_list_1 : statement_1;
...
constant_list_n : statement_n
end
Design choices:
1. Expression is any ordinal type (int, boolean, char, enum)
2. Segments can be single or compound
3. Construct is encapsulated
4. Only one segment can be executed per execution of the construct
5. Result of an unrepresented control expression value is undefined
#include <stream.h>
int days_in_month(int month) {
int days;
switch (month) {
case 2:
days = 29;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
default:
days = 31;
}
return days;
}
void main() {
cout << days_in_month(3) << endl;
}
72
Iterative Statements
73
Counter-Controlled Loops
Design Issues:
74
C- Syntax
C Design Choices:
75
C++ and Java
C++ : Differs from C in two ways:
76
Logically-Controlled Loops
Design Issues:
1. Pretest or posttest?
2. Should this be a special case of the counting loop
statement (or a separate statement)?
- Language Examples:
1. Pascal has separate pretest and posttest logical loop
statements (while-do and repeat-until)
2. C and C++ also have both, but the control expression
for the posttest version is treated just like in the pretest
case (while - do and do - while)
3 Java is like C, except the control expression must be
Boolean (and the body can only be entered at the
beginning)
77
User-Located Loop Control Mechanisms
Design issues:
1. Should the conditional be part of the exit?
2. Should the mechanism be allowed in an already controlled loop?
3. Should control be transferable out of more than one loop?
Examples:
1. Ada - conditional or unconditional; for any loop;
any number of levels
78
User-Located Loop Control Mechanisms
FORTRAN 90 - EXIT
Unconditional; for any loop, any number of levels
FORTRAN 90 also has CYCLE, which has the same semantics as C's
continue
79
Iteration Based on Data Structures
80