27
159.331 Prog Lang & Alg 1 Programming Languages Imperative Programming Languages - Part 1 - Principles & Data

Programming Languages

Embed Size (px)

DESCRIPTION

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: - PowerPoint PPT Presentation

Citation preview

159.331 Prog Lang & Alg 1

Programming Languages

Imperative Programming Languages - Part 1 - Principles & Data

159.331 Prog Lang & Alg 2

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

159.331 Prog Lang & Alg 3

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

159.331 Prog Lang & Alg 4

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;

159.331 Prog Lang & Alg 5

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

159.331 Prog Lang & Alg 6

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;

159.331 Prog Lang & Alg 7

Uninitialised VariablesNon-initializing declaration creates uninitialized variable

Different languages provide different safety treatments:1. Problem ignored, it’s the programmer’s responsibility - inelegant

2. Uninitialized variable declared erroneous and it is compiler writer’s responsibility to enforce the check - expensive

3. 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

4. Disallow non initializing declarations - forces programmer to deal with the issue

5. 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

159.331 Prog Lang & Alg 8

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…

159.331 Prog Lang & Alg 9

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

159.331 Prog Lang & Alg 10

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

159.331 Prog Lang & Alg 11

159.331 Prog Lang & Alg 12

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

159.331 Prog Lang & Alg 13

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

159.331 Prog Lang & Alg 14

Enumeration Types• Simplest type constructor in enumeration

• No new type just a binding of names to set of values in an existing type

In C:

typedef enum {

red, amber, green

} traffic_light_colour;

traffic_light_colour n, e, s, w;

typedef enum{ false, true } boolean;

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.

159.331 Prog Lang & Alg 15

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

159.331 Prog Lang & Alg 16

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

159.331 Prog Lang & Alg 17

Array SyntaxIn 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)

159.331 Prog Lang & Alg 18

More Array SyntaxIn 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;

159.331 Prog Lang & Alg 19

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 copy function memcpy() to do this.

159.331 Prog Lang & Alg 20

Some languages have multi-dimensional arrays and some alsoarrays 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

159.331 Prog Lang & Alg 21

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

159.331 Prog Lang & Alg 22

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)

159.331 Prog Lang & Alg 23

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

159.331 Prog Lang & Alg 24

Sequences or Listssequences 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

159.331 Prog Lang & Alg 25

Setssets 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.

159.331 Prog Lang & Alg 26

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.

159.331 Prog Lang & Alg 27

Imperative - Part 1 -Summary• 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