imperative programming part one. 2 overview outline the characteristics of imperative languages...

25
Imperative Programming Part One

Upload: charleen-higgins

Post on 27-Dec-2015

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Imperative Programming

Part One

Page 2: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

2

Overview

Outline the characteristics of imperative languages

Discuss other features of imperative languages that are not in Clite.

Page 3: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

3

Imperative Languages

Imperative languages are characterized by 3 properties: the sequential execution of instructions, the use of variables representing memory

locations, and the use of assignment to change the values of

variables

Its primary feature is a sequence of statements representing commands (imperatives).

Page 4: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

4

Example

Page 5: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

5

Imperative Languages A programming language is said to be Turing complete if it

contains integer variables, values, and operations has assignment statements has control constructs of

statement sequencing, conditionals and branching statements.

An imperative programming language is one which is Turing complete and supports the following features: Data types for real number, characters, string, boolean etc; Control structures, for and while loops, case (switch) statements; Arrays and element assignment;

Page 6: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

6

Naming and Variables

Names or identifiers in Clite denote locations in the memory composed of letters and digits; the first character

must be a letter Recall: [a-z A-Z] [a-z A-Z 0-9]*

no restriction on number of characters (must fit in one line)

case-insensitive; fib, Fib, and FIB are all the same.

Page 7: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

7

Naming and Variables (2)

Some names in Clite are reserved: they may not be used for variable names e.g. int, boolean, while, if, else, … etc.

Modern languages try to limit their reserved words.

Page 8: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

8

Naming and Variables (3)

In some languages, some names are merely predefined. programmers are free to redefine them such feature minimizes the number of reserved

words, but also may lead to confusion!var true: boolean;begin

true := false;…end

Page 9: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

9

Naming and Variables (4)

In Clite, each location in the memory is declared with a unique name associated with type assigned with value

A valid Clite program cannot declare two different variables with the same nameV(Declarations d) = i, j {1,…, n}:(i j di.v dj.v)

Page 10: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

10

Elementary types & values

A Clite type is defined as a set of values and a set of operations on those values.

The basic types in Clite are: int, float and boolean.

type Int and float boolean

values {…, -1, 0, 1, …} {true, false}

operations

arithmetic

+, , *, /&&, ||, !

relational ==, !=, , =, ,

=

Page 11: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Elementary types in real languages Data types in programming languages can be

partitioned into two classes: elementary types: denote data values that can be

stored in fixed memory space. structured types: require a variable memory

space to store their values.

Page 12: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Elementary types in C++, Ada, & Java

Type C/C++ Ada Java

Byte byte

Integer short, int, long

integer short, int, long

Real number float, double, long double

float, decimal

float, double

Character char character char

Boolean bool boolean boolean

Pointer * access

Page 13: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Memory units

Memory requirements for the above different types are based on the following memory units: Nibble: 4 bits Byte: 8 bits = 1 byte byte Half-word: 16 bits = 2 bytes short Word: 32 bits = 4 bytes int, float Double word: 64 bits = 8 bytes long, double Quad word: 128 bits = 16 bytes

Page 14: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Strings

The language C does not recognize character strings as a distinct data type. strings are encoded as arrays of ASCII characters one character per byte terminated by a null character (all bits zero) E.g. The string “hello” would be encoded as:

68 65 6c 6c 6f 00 … If the size of the array is greater than the size of

the string + 1 then the remaining elements of the array are undefined

Page 15: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Strings (2)

In the language Java, String is a language-defined object type its exact representation is hidden from the

programmer stored in Unicode using 2 bytes per character E.g. The string “hello” would be encoded as:

00 68 00 65 00 6c 00 6c 00 6f the left-hand byte of every ASCII character in

Unicode is zero (hex 00)

Page 16: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Expressions in real languages In actual programming languages, there is a

richer variety of operators, and hence a richer variety of expressions.

Refer to the textbook for a list of available operators in C, C++ and Java.

Page 17: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Conditional operator :?

C, C++ and Java use unusual operator :? It embeds a complete “if…then…else”! General form:

<exp1> ? <exp2> : <exp3> E.g.

c = (a > b) ? a : b;is equivalent to:if (a > b) c = a;else c = b;

Page 18: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Operator overloading, conversion, & casting C, C++, and Java support overloading their

operators.

An operator is overloaded if it can be used in different context with different types of operands and compute different results

Page 19: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Operator overloading, conversion, & casting (2) Consider, k = i / j;

In Clite, this expression always carry the same meaning (int, float).

In C/C++/Java, the outcome depends on the types of i, j, and k, and possible intermediate conversions

A conversion (or casting) translates data values form one type to another.E.g. int value 1 is equivalent to float value 1.0

Page 20: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Operator overloading, conversion, & casting (3) Explicit conversion can be specified using the cast operator.k is int, i and j are double:

k = (int) i/j

Here, we are overriding the warning that information may be lost and saying it’s ok to truncate the double quotient.

Page 21: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Syntax and Semantics of Statements in Real Languages

Imperative languages offer wider variety of statement types.

Common other statements includes: other forms of loop statements other forms of conditional statements …

Page 22: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

For Loops

Most imperative languages have a counting loop statement.

A simple C/C++/Java-like counting loop is called a for loop.

The concrete syntax of a for loop is as follows:ForStatement

for(Assignment1opt; Expressionopt; Assignment2opt) Statement

Page 23: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Do Statements

Another type of looping construct is called do statement. the body of the loop is guaranteed to execute at

least once. this is useful to initialize variables used in the loop

test. Concrete syntax:

DoStatement do Statement while (Expression)

Page 24: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Switch Statements

In addition to simple IfStatements, most imperative languages provide a multiway conditional statement such as switch statements.

Concrete syntax:SwitchStatement switch (Expression) {Cases}Cases Case Cases | Case | Default Case CaseHead Case | CaseHead StatementsCaseHead case Literal :Default default : Statements

Page 25: Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are

Switch Statements (2)

Within the above constraints, we can model the SwitchStaement:

switch(e){

case v1: s1

case vn: sn

default: sn+1

}

as a series of IfStatements:

if(e==v1) s1

else if (e==vn) sn

else sn+1