Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are."— Presentation transcript:

1 Imperative Programming Part One

2 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are not in Clite.

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

4 4 Example

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

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

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

8 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

9 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 name V(Declarations d) =  i, j  {1,…, n}:(i  j  d i.v  d j.v)

10 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 floatboolean values{…, -1, 0, 1, …}{true, false} operations arithmetic +, , *, / &&, ||, ! relational ==, !=, ,  =, ,  =

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

12 Elementary types in C++, Ada, & Java TypeC/C++AdaJava Byte byte Integer short, int, long integershort, int, long Real number float, double, long double float, decimal float, double Character charcharacterchar Boolean boolboolean Pointer *access

13 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

14 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

15 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)

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

17 Conditional operator :? C, C++ and Java use unusual operator :? It embeds a complete “if…then…else”! General form: ? : E.g. c = (a > b) ? a : b; is equivalent to: if (a > b) c = a; else c = b;

18 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

19 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

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

21 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  …

22 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(Assignment1 opt ; Expression opt ; Assignment2 opt ) Statement

23 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)

24 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 Statements CaseHead  case Literal : Default  default : Statements

25 Switch Statements (2) Within the above constraints, we can model the SwitchStaement : switch(e){ case v 1 : s 1 … case v n : s n default: s n+1 } as a series of IfStatements : if(e==v 1 ) s 1 … else if (e==v n ) s n else s n+1


Download ppt "Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are."

Similar presentations


Ads by Google