270 likes | 439 Views
Programming Languages. Imperative Programming Languages - Part 1 - Principles & Data. Principles of Imperative Languages. Procedural or Imperative Languages involve: Data & Data declarations State changing commands (or statements) Stepping the flow of control We will also look at:
E N D
Programming Languages Imperative Programming Languages - Part 1 - Principles & Data Prog Lang & Alg
Principles of Imperative Languages Procedural or Imperative Languages involve: • Data & Data declarations • State changing commands (or statements) • Stepping the flow of control We will also look at: • Program Composition • Examples of Imperative Programming Prog Lang & Alg
Data • All data in a computer has some representation and number of properties - known as its type • Most data items have names • Relationships between data items, names, types and representations specified by the programmer in data declarations Prog Lang & Alg
Data Declarations • Data item in memory has number of bits • Need a structure to know how to interpret the bits appropriately. • Usually needs a name so we can refer to it too • A data declaration binds a name to the (particular) data item and its structure • Data in the machine can be changed - hence we call it a variable - with name, type and value • Note that a data item exists in the machine • Its structure and name exist only in the program • In C: int i, j; Prog Lang & Alg
Data Initialization • Some languages allow us to combine data declaration with initialization • In C: int i=5, j=5; • C has facility to initialize different variables in single declaration to different values • Remember the declaration is doing two things: • Creating or Allocating memory for the data item • Binding a name to it Prog Lang & Alg
Constant Declarations • Some languages have constant declarations so that the variable’s bit pattern cannot be changed once it is initialized. • In C: const int i = 42; Prog Lang & Alg
Uninitialised Variables Non-initializing declaration creates uninitialized variable Different languages provide different safety treatments: • Problem ignored, it’s the programmer’s responsibility - inelegant • Uninitialized variable declared erroneous and it is compiler writer’s responsibility to enforce the check - expensive • All uninitialized variables silently initialized to some default value eg zero bit pattern, pointers set to null - creates false sense of security for the programmer • Disallow non initializing declarations - forces programmer to deal with the issue • For each type have a special representation meaning “not initialized - often called the omega value - this propagates and allows dynamic checking but changes meaning of “uninitialized” Most languages fully or partially ignore the problem Prog Lang & Alg
Renaming & Aliasing • A second sort of declaration - a renaming or aliasing one • In Ada: K: Integer renames I; • No extra storage allocated - just another name bound to same variable • Potentially dangerous - can have surprise side effects • Some parameter transfer mechanisms need this feature • No such facility in C - not the same as a pointer… Prog Lang & Alg
Overloading • The opposite or counterpart of aliasing • Aliasing - multiple names bound to one name • Overloading - binds more than one object code to the same name eg “+” works for ints and floats • Which actual object code used will depend upon context - enough contextual info must be present to resolve the ambiguity • C uses overloading for built-in operators only • Ada has elaborate set of resolution rules Prog Lang & Alg
Types & Type Constructors • Hardware: type is a way of interpreting data bits • Programming: type specifies set of allowed values and set of operations defined on those values • Task of language compiler writer to find most efficient hardware representation for a given language • Basic types or primitive types in many languages • Characters - eg char • Integers eg short, int, long, long long • Floating point numbers - eg float, double Prog Lang & Alg
Literals • Explicit values of the basic types are written down “literally” in a program - hence the term literal • eg -123 or 3.14156984 or ‘K’ or “Ken” • Sometimes we need some extra syntax • eg 0x19AF (Hex) or 0123 (Octal) • or 1234L (long) or 3.14F (float) • Void is technically a type - often used to specify set of no values or a related idea Prog Lang & Alg
Type Constructors • The primitive types usually built into a language and set up automatically • The user/programmer can specify their own types - use type construction • In C: typedef int int_10_array[10]; int_10_array ia; • In Ada: type Int10-Array is array (Integer range 1.. 10) of Integer; IA: Int10_Array; • Data types composed of other types are called compound types Prog Lang & Alg
Enumeration Types • Simplest type constructor in enumeration • No new type just a binding of names to set of values in an existing type In Ada: Type Traffic_Light_Colour is (Red, Amber, Green); N, E, S, W: Traffic_Light_Colour; Type Boolean is (False, True); Actually Boolean is a built-in type in Ada. • In C: • typedef enum { • red, amber, green • } traffic_light_colour; • traffic_light_colour n, e, s, w; • typedef enum{ false, true } boolean; Prog Lang & Alg
What can we do with enum types? • Might expect just to be able to do copying and comparison for equality • Most languages implement them as integers and let you do comparisons (greater/smaller) or incrementing them (successor operation) • This makes sense for some enumerated types eg “days of week” or “months of year” or character set with lexical ordering eg ASCII or Unicode Prog Lang & Alg
Arrays • Simplest type constructor that builds upon another type • Array is a series of a known number of items - all of the same type • An item is called an element • Accessed by an ordinal known as its index • Array represents a mapping from some type(typically integers) to values of the element type • Language might supply operators for lower bound, upper bound and size of an array Prog Lang & Alg
Array Syntax In C: • int ia[10] • First element is ia[0], last element is ia[9] • sizeof(ia) gives size in bytes, so use: (sizeof(ia ) / sizeof( ia[0] ) ) to get number of elements • Initialisation requires a compound value (called an aggregate in Ada) • eg int ia[3] = {3, 5, 8}; • Note all our bounds here are constants - ie they are known at compile time (known as static bounds) • Some languages have dynamic bounded arrays (Ada does, Java does, but not C) Prog Lang & Alg
More Array Syntax In Ada: • IA: array (Integer range 1..10) of Integer; • IA(1) is first element, IA(10) is last element • IA’First is lower bound • IA’Last is upper bound • IA’Length is the length or size of the array • Initialising: IA: array (Integer range 1..3) of Integer := (3, 5, 8); • Dynamic bounding: IA: array (Integer range M..N-1) of Integer; Prog Lang & Alg
Flexible Arrays • flexible arrays can be resized dynamically • Neither Ada nor C has this feature although some languages like Algol, Orca and Icon do • In C we can simulate this feature using library functions malloc() and realloc() • Note this may involve copying data around to new locations in memory to ensure array is contiguous in its new memory location. This can be slow - sometime use special fast copyfunction memcpy() to do this. Prog Lang & Alg
Some languages have multi-dimensional arrays and some also arrays of arrays. Common Syntax is iaa[3,5] for multi-dimensional arrays Ada array-of-arrays must have same bounds (sizes) so less useful. ANSI C uses [][] notation for indexing multi-dimensional arrays Prog Lang & Alg
Row and Column Major • Some algorithms step through multi dimensional data in nested repetition - it is important to known how data is stored in memory to get best data caching performance • C is “row major” (So are C++ and Java) So in an array myArray[i][j], think of j is the one that moves fastest in memory, i moves more slowly. • Fortran is “column-major” - the opposite - comes from Fortran’s roots in maths and matrix terminology - Matrix element Aij row i, column j Prog Lang & Alg
Slicing • array slicing is similar to indexing, but accesses a subarray rather than a single element • In Ada the elements IA(3) through IA(6) can be accessed (in one go) as the slice IA(3..6) • C has no slicing. • Modern data-parallel Fortrans use slicing to good effect to specify chunks of data that can be distributed across processors in parallel computer system. • Some languages like Fortran90 allow you to specify a stride in the slice specification • A(4:10:2) means the slice containing A(4), A(6), A(8) and A(10) Prog Lang & Alg
Associative Arrays • Array mapping need not be just with integer index values • As long as we can resolve ambiguities, we can use any mapping (in principle - although may be inefficient) • Associative arrays use potentially arbitrary types for the index (the language ABC supports this) • They associate an index value of one type with elements in the array of a second type • Supported in many scripting languages eg Perl and Python to index into an array using a string as the “index” • C and Ada allow indexing with enumerated types and characters ( they map to integers anyway) • Other languages support this with library functions - eg C++ Standard Template Library, or java.util package Prog Lang & Alg
Sequences or Lists sequences or lists are constructed types that build a new type using only one base type. • Unlike arrays they hold an unknown number of elements - all of the same type • First element can be accessed using a head operator; the rest using a tail operator • No simple way to determine the size or length of the list (or indeed the last element) • If there were, these would just be “arrays in disguise” • Arrays and sequences are often confused by programmers • Strings are generally used as sequences of characters, although often implemented (modelled) as arrays with messy function calls in a library Prog Lang & Alg
Sets sets also build a new type using only one type • A set over type T with set of values V is a type whose values are sets of the elements of V • Usual operators are set-union, set-difference and set-intersection • Neither C nor Ada provides sets. Modula-2 does. C++ and Java provide set libraries. • number of elements in a set is its cardinality • A powerset operator yields the set of all sets of its argument set. The SETL language has this. Prog Lang & Alg
Bags • Bags are variants of sets - they can contain a value more than once • They record how many times a value has been inserted • Primary associated operations are insert-value and remove-value • Sometimes known as multi-sets • The language ABS has bags. C and Ada do not, C++ and Java have multi-set libraries. Prog Lang & Alg
Data & Data Declarations Constant Declarations Uninitialized variables Renaming & Aliasing Overloading Types and Type Constructors Enumeration Types Arrays Sequences Sets & Bags Next - Records, Pointers, and Unions Bal & Grune Chapter 2 - Sections 2.1 and 2.2 Sebesta Chapters 5 & 6 Imperative - Part 1 -Summary Prog Lang & Alg