1 / 79


Coverage. Variables Type Checking Scope Referencing Environments Data Types Arithmetic Expressions Control Flow Stack and Subprograms Programming for Imperative Features. Introduction.

Download Presentation


An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.


Presentation Transcript

  1. Coverage • Variables • Type Checking • Scope • Referencing Environments • Data Types • Arithmetic Expressions • Control Flow • Stack and Subprograms • Programming for Imperative Features Imperative Languages

  2. Introduction • Imperative languages are based on the concept of Von Neumann machines where memory is separated from Central Processing Unit (CPU); data and programs are stored in the same memory. • Instructions and data are piped from memory to CPU for further processing. • Von Neumann machines have general purpose registers and Arithmetic Logic Unit (ALU). • Instruction cycle proceeds by fetching the instruction, updating the program counter and then executing the instruction. • Direct Memory Access (DMA) provides transfer of data from or to Input-Output (I/O) devices and to or from memory by stealing memory cycles from CPU. Imperative Languages

  3. Variables Languages is said to be turing complete if it has integer variables, integer values, integer operations, assignment statements, statement sequences, conditionals and branching statements Names or identifiers are used to refer to variables, procedures, constants, etc User-defined Names or identifiers Length, Case sensitivity COBOL: maximum variable length is 30 characters FORTRAN 90 and ANSI C: maximum 31 characters Ada, C# and Java: There is no limit on the length C++: no limit, but implementers often impose one C, C+ and Java: Case sensitive in nature 3 Imperative Languages

  4. Variables Pascal and C: Variables are to be declared before use The name of a variable is used to identify and refer to the variable. Scope of a variable is the segment of the program within which it is defined. Visibility of a variable is visible within some sub-segment if it can be directly accessed using its name. Lifetime of a variable is the time period from the allocation of memory to the variable to when the memory is de-allocated, during program execution. Lifetime is a dynamic property while scope and visibility are static properties Address is the memory location associated with the variable and it is called as l-value (left value or locator value) Value (called as r-value or right value) is the actual contents of the memory location of the variable 4 Imperative Languages

  5. Variables Type of the variables controls the range of values and the set of operations that could be performed The byte data type in Java is of one byte with values ranging from -128 to 127; short data type in Java is of two bytes with data ranging from -32768 to 32767 The process of associating an attribute to an entity is called as Binding. The process of associating an operation and a symbol is also called as Binding. Language design time: Also called Language definition time Programmer is given a definite set of syntax and program structures Example: Operators with their respective operations, data types, … In C language, new data types can be defined, ex., struct. 5 Imperative Languages

  6. Binding Time Language implementation time: Binding might vary among different implementations of the same language Depends on hardware architecture and operating system Floating point data type representation Compile time: Also called as Translation time binding Binding of variables to its type in C or Java Allocation of memory for arrays or relative memory allocations Load time: Also called Link time Binding the variables to the memory cell during loading time as in static variables of C language and Fortran 77 variables Task is handled by loader or linker Runtime: Also called Execution time binding Binding non-static variable to the respective memory cell is done at runtime Binding of formal to actual parameters needs to be done at runtime 6 Imperative Languages

  7. Variable Binding The process of binding a variable during compile time or load time or at the beginning of runtime is called early binding. If the binding happens during later stages of the runtime, then it is called late binding. In C#: Generic data type Object is bounded using late binding while a specific object type is bounded using early binding Static binding occurs before run time and remains unchanged throughout program execution Dynamic binding occurs during execution or can change during the execution of the program JavaScript and PHP use dynamic binding 7 Imperative Languages

  8. Variable Binding Dynamic binding example in JavaScript <script type="text/JavaScript"> var myList = [1,2,3]; document.write("myList[0] = " + myList[0]+ " "); // 1 var myInteger = 45; document.write("myInteger[0] = " + myInteger[0]+ " "); // undefined document.write("myInteger = " + myInteger+ " "); // 45 myList = myInteger; // List is overwritten by the integer element document.write("myList[0] = " + myList[0]+ " "); // undefined document.write("myList = " + myList+ " "); // 45 var myArray = new Array(); myArray[0]="India"; myArray[1]="Brunei Darussalam"; myList = myArray; // List overwritten by the array elements. document.write("myList[0] = " + myList[0]+ " "); // India document.write("<BR>"); </script> Doing something similar in C or C++ or Java will generate an error. 8 Imperative Languages

  9. Variables Type inferencing Type of the variable is based on the context of the reference and not by the assignment statement Used in ML, Miranda and Haskell Variable type declaration Explicit: Specified with a statement Implicit: Inferred from the first assignment Example: FORTRAN, PL/I, BASIC and Perl Lifetime of a variable Static: Variable is bound to a memory cell before execution and it remains bounded to the same memory variable throughout execution Example: Fortran 77 variables, C or C# static variables Stack-dynamic: Variable is bound to a memory cell during the process of elaboration of the declaration of the variable. This process happens during program execution Example: local variables in C subprogram and Java methods Variables defined in methods of Java or C++ or C# are by default stack-dynamic. 9 Imperative Languages

  10. Lifetime of a variable Lifetime of a variable Explicit heap-dynamic: Allocation and de-allocation are done explicitly using certain directives from the programmer. They are referenced through pointers or references Example: all objects in Java and dynamic objects created in C++ using new and delete methods In C#, objects defined using class keyword are allocated on the heap but those defined using struct keyword are stack allocated. Implicit heap-dynamic: Allocation and de-allocation is caused by assignment statements Example: all variables in APL (A Programming Language), all strings and arrays in Perl and JavaScript. 10 Imperative Languages

  11. Pointer variable Pointers are used to refer to memory locations where variables are stored Used in Pascal [called as dynamic variables, referred using ^ operator], C, C++, C# [with unsafe], etc In C: & is used to refer to memory location and * is used to refer to the contents of the memory location In C, void * is a generic pointer. Generic pointer cannot be dereferenced and thus pointer arithmetic operations can not be applied on them 11 Imperative Languages

  12. Pointer variable #include <stdio.h> int main() { int a = 5; int b = 10; int *ptra = &a; // ptra is a pointer to a int *ptrb = &b; // ptrb is a pointer to b *ptra = *ptrb; // a and b will have the same value printf("*ptra = %d, *ptrb = %d\n", *ptra, *ptrb); printf("a = %d, b = %d\n", a, b); if (ptra == ptrb) printf("%s", "ptra and ptrb are the same\n"); else printf("%s", "ptra and ptrb are not the same\n"); if (*ptra == *ptrb) printf("%s", "*ptra and *ptrb are the same\n"); ptra = ptrb; // pointers now point only to b printf("a = %d, b = %d\n", a, b); Return 0; } 12 After the statement: ptra = ptrb; Imperative Languages

  13. Type Checking Make sure that the operands and the operator are of compatible types Errors detected during type checking are called as type errors Process of conversion of one type to another by the compiler itself is called as coercion If all type bindings are static, nearly all type checking can be static. If type bindings are dynamic, then type checking must also be dynamic. A language which could catch all type errors is called strongly typed language. 13 Imperative Languages

  14. Type Checking Holding different values at different times Pascal: variant records C or C++: union inside struct In C and C++, parameter type checking can be avoided and unions are not type checked Ada and Java are almost strongly typed languages 14 Imperative Languages

  15. Type Checking ML and Haskell are strongly typed languages Fortran 77, Pascal, C and C++ are not considered as strongly typed languages Languages that are without static type systems are called as untyped languages or dynamically typed languages Example: Scheme, Smalltalk, Perl, … Static or Translation-time Type Checking Checking for type errors at translation stage Will indicate type errors earlier Attaching types to expressions is called as type inferencing int main( ) { float value; value = 10/3; printf("%f\n",value); return 0; } This program returns 3.000000. To get 3.333333, we need to use 10.0/3 or 10/3.0 15 Imperative Languages

  16. Type Equivalence Type restrictions on structured types In Pascal, formal parameters (parameter present in the subprogram) must be of the same type as their corresponding actual parameters (parameter present in the subprogram call). In Pascal, Subranges, which has a limited range of allowed values of integer types, is not compatible with integer types For example, a range 10 ... 12 is a subrange of integer type Subrange type is not present in C++ or Java. Structure type compatibility or structure equivalence Equal only if they have identical structures Two records having the same structure but different field names should be considered compatible or not. Two array having the same type but different subscript should be considered compatible or not. Two enumerations with their components spelled differently should be considered compatible or not. 16 Imperative Languages

  17. Type Equivalence C language: Following two arrays are structurally equivalent. Same size and same content int a[3] = {1,2,3}; int b[3] = {1,2,3}; Name equivalence: Needs to be of same name C language: struct does not imply structure equivalent struct x { int a; }; struct y { int a; }; typedef struct {int x1; char z;} Rec1; typedef Rec1 Rec2; typedef struct {int x2; char z;} Rec3; int main() { struct x z, a2; struct y a1; z = a2; z = a1; // z = a2 has no error but z = a1 has compile error Rec1 a, b; Rec2 c; Rec3 d; a = b; // Name equivalence a = c; // Name equivalence // a = d; // Generates compilation error. } 17 Imperative Languages

  18. Type Equivalence Declaration equivalence Must have the same original structure if traced backwards a and b are considered declaration equivalent. int a[10], b[10]; typedef char * string; string a; char *b; Pascal Parameter type checking is using Name Equivalence Other types check is upto the implementation 18 Imperative Languages

  19. Type Equivalence C or C++: Supports structured type compatibility except for records or struct Java: Does not support structure equivalent in classes. class C1 { int x, y; } class C2 { int x, y; } public class Equivalence {public static void main(String[] args) { C1 a = new C1(); C1 c = new C1(); a = c; // works ok. C2 b = new C2(); a = b; // Compilation error - incompatible. C1 z = new C2(); // Compilation error. } } 19 Imperative Languages

  20. Scope Scope of any variable is the range of statements over which it is visible Lexical scope: Depends on where the variable is defined. Static Scope Depends on the location of the declaration of the variable Local variables override global variables Handled during translation or compile time itself Dynamic Scope: Based on the flow of execution of the program Dynamic scope makes the reference convenient but readability is affected. Scope and lifetime are sometimes closely related but are different concepts 20 Imperative Languages

  21. Scope What is the output of this C program? x is printed as 110 as static scope is used. 21 Imperative Languages

  22. Referencing Environments Referencing environment of a statement indicates the collection of all names that are visible in the statement Static-scoped language: local variables and visible variables of the enclosing scopes Dynamic-scoped languages: local variables and visible variables of all active subprograms Components of referencing environment: Local referencing environment: Called as local environment Formal parameters, local variables and subprograms that are defined as part of current subprogram Global referencing environment: Global associations that are available during the current execution 22 Imperative Languages

  23. Referencing Environments Components of referencing environment: Non-local referencing environment: Associations that are not created on entry into subprogram Predefined referencing environment: Associations defined in the language definition itself Named constant: Value is bound to the variable only when the storage is bounded Can be done statically (called as manifest constants) or dynamically Pascal has literals only Fortran 90 has constant-valued expression where all terms are either literal or named constants; Ada and C++ allow named constants to be dynamically bounded to values. // C++ named constant dynamically bounded. const int area = side * side; 23 Imperative Languages

  24. Referencing Environments Java has named constants dynamically bounded, with an additional reserved word final. class Constants { public static void main(String[] args) { int side = 20; final int length = 20; final int area = side * side; System.out.println(length); // Prints 20 System.out.println(area); // Prints 400 } } In C#, there are two types of named constants: const and readonly. Const-named constants have value assigned at compile time and can not be changed once established Readonly-named constants have value assigned at run time and can not changed once established Readonly-named constants can be assigned in a constructor but const-named constants can not be assigned in a constructor 24 Imperative Languages

  25. Referencing Environments In C#, there are two types of named constants: const and readonly. Readonly is an access privilege where the value can be changed from the owner but not from outside. const double PI = 3.14159; readonly double PI = 3.14159;  Binding of a variable to a value at the time it is bound to the storage is called initialization. Initialization is mostly done during the declaration statement. For example, in Java, int sum = 0; 25 Imperative Languages

  26. Data Types Various data types: primitive data types, character string types, user-defined ordinal types, array types, associative arrays, record types, union types and pointer types Primitive Data Types Not represented or defined in terms of any other data type Called as Scalar Structure as only one occurrence of data type is held at any time Integer: signed or unsigned; byte, short, int, long Floating point: IEEE 754 Floating Point representation Represented using sign, exponent and mantissa C language: float (32 bits), double (64 bits) Decimal: Numbers with fixed digits like money, cost, etc Boolean: bits represented as bytes Character: numeric codes like ASCII or Unicode 26 Imperative Languages

  27. Data Types C language does not have Boolean data type: typedefenum {false, true} bool; int main() { inti = 10; typedefenum {false, true} bool; boolbv; bv = i + 20; printf("Integer Value = %d\nBoolean Value = %d\n", i, bv); return 0; } The boolean variable bv is stored with an integer number 30. 27 Imperative Languages

  28. Boolean Data Type C++ language with bool data type: Non-zero (including negative values) represent true and zero represents false #include <iostream> // iostream.h is depreciated using namespace std; // needed for cout to work // if namespace std is not used, cout must be represented as std::cout // similarly, all functions should be with respective namespace int main() { int i = 10; bool bv = true; cout << bv << endl; bv = i + 20; cout << bv << endl; return 0; } This program returns the binary value as 1, even after the assignment statement. This is because C++ considers any non-zero value as 1. In C#, assigning an integer value to bool variable gives an error. It should be assigned as: bool variablename = true; 28 Imperative Languages

  29. String Data Type Sequence of characters and so a non-scalar structure C language: String is considered as a character array #include <stdio.h> int main() { char x[10]; // x[0] = "H"; // No compilation error, but nothing is printed. // Usage of double-quotes is wrong. x[0] = 'H'; x[1] = 'e'; x[2] = '\0'; printf("%s\n",x); return 0; }  29 Imperative Languages

  30. User-defined Ordinal Types Ordinal types are of a set provided in a discrete order Discrete order is of first element, last element and the presence of previous and next elements Enumeration type Consists of all possible values that are considered as symbolic constants C or C++ language: enum Name {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; enum Name x = Sat; // x is the variable Name x; // x is the variable – Not allowed in C x++; // Allowed in C but not in C++ int a = Sat; printf("%d\n",a); // Allowed in C and C++ // this is equivalent to #define Zero 0; #define One 1 enum {Zero, One}; printf("%d\n",Zero}; // Prints 0 C# language: public enum Days {Mon, Tue, Wed, Thu, Fri, Sat, Sun}; 30 Imperative Languages

  31. User-defined Ordinal Types Subrange type Users should provide the ordered contiguous subsequence of an ordinal type. In Pascal: type Month = 1 .. 12; 31 Imperative Languages

  32. Arrays Collection of homogeneous data elements, positioned relative to the first element Called as homogeneous structures If the array index crosses the boundary of the array, an array boundary error occurs. Exception to this is COBOL language. Issues in handling arrays: What types are considered legal subscripts? If the subscript is represented as an expression, are they checked for range? When does the language allocate space for the array? How many subscripts are allowed for an array? Is it a must to initialize the array objects? Can we allocate the subscript in two different ranges? 32 Imperative Languages

  33. Arrays Types of Arrays: Static Array: Storage allocation is static and subscript range is statically bounded No allocation or de-allocation of locations during runtime. Fixed stack dynamic array: The range is statically bounded but the allocation is done during execution. Stack-dynamic array: Allocation is dynamic and subscript range is also dynamically bound. But, once the range is fixed, it stays as it is. Fixed heap-dynamic array: Subscript range is dynamic and allocation is also dynamic but once they are bounded, they stay as it is. Heap-dynamic array: Subscript range and allocation are dynamic and changes could be done any number of times during the lifetime of the array. 33 Imperative Languages

  34. Arrays Arrays declared as static in C or C++ functions are static arrays Arrays not declared as static in C or C++ functions are fixed stack-dynamic arrays Arrays in Ada language are stack dynamic Arrays in Java are fixed heap dynamic arrays Heap-dynamic arrays could be created in C# using the class ArrayList. With regards to array initialization: Perl does it as: @a = (1..12); C and C++ language: int group [] = {1,2,3,4,6}; C# language: int[] group = new int[]{1,2,3,4,6}; 34 Imperative Languages

  35. Arrays Array slicing: Slice is some substructure of an array. It could be used if there are certain operations to be performed on a part of the array. In Perl, @_ is the list of incoming parameters to a sub. sub sortArray { # @_ stands for the parameter passed to the sub. my $n = @_ - 1; @_[0..$n] = sort(@_[0..$n]); } C and Java have array index starting with 0 Unlike C, Java allows the size of the array to be specified dynamically because it is dynamically heap allocated Once the array size is specified, the size cannot be changed in Java. 35 Imperative Languages

  36. Associative Array Unordered collection of data elements that are indexed by an equal number of values called keys In Perl, names begin with % and literals are delimited by parenthesis. %hi_temps=("Monday"=>1,"Tuesday"=>2,…); Subscripting is done using braces and keys $hi_temps{"Wednesday"} = 3; Elements can be removed with delete delete $hi_temps{"Tuesday"}; 36 Imperative Languages

  37. Associative Array Associative arrays can be implemented in C++ as: #include <string> #include <map> #include <iostream> int main() { // studentrecord is a map from string to int std::map<std::string, int> studentrecord; studentrecord["Hazwan"] = 11; // Hazwan's ID is 11 studentrecord ["Atul"] = 12; // Atul's ID is 12 std::cout << "Atul's ID is " << studentrecord["Atul"] << std::endl; } 37 Imperative Languages

  38. Records and Unions Record is a heterogeneous aggregate of data elements in which each individual element is identified by its name. C++ struct is an example of records Union is similar to record by grouping heterogeneous elements but only one value is stored at a time In C++, union within struct definition is called as anonymous union. Java does not possess both records and unions. Ada has a safe union mechanism called variant record where the discriminant (which gives the type of data being stored) and the actual value are stored at the same time Example when different information is stored at different time: For a train travel, the speed is applicable only when the train is travelling between stations. But when the train stops at a station, the speed element is not applicable Using arrays to store the information will waste spaces when speed is not applicable 38 Imperative Languages

  39. Records and Unions Pascal supports variant records, which could store different values of different types at different times program main(input,output); type day = (weekday,weekend); var employee : record name : String; dept: String; salary: real; case daytype : day of weekday: (officephone : integer); weekend: (housephone : integer; houseaddress : String); end; begin employee.name:='Seyed Abdur Rahman'; employee.dept:='Language'; employee.salary:= 20000.00; employee.daytype:=weekday; employee.officephone:= 23333; writeln(employee.name,' ',employee.dept); writeln(employee.officephone); writeln(employee.salary:6:2); {there should be no housephone because daytype=weekday} // the value printed is same as officephone writeln(employee.housephone); employee.daytype:=weekend; employee.housephone:= 22222; employee.houseaddress:='Mohideen Street, Kayalpatnam'; writeln(employee.housephone,' ',employee.houseaddress); end. 39 Imperative Languages

  40. Records and Unions Example in C language: typedef union { int marks; char grade; } uniondata; typedefstruct { char *name; int id; uniondata details; } StudentRecord; int main() { StudentRecordsturec; sturec.name = "NoorulHaq"; sturec.id = 123456; sturec.details.marks = 84; sturec.details.grade = 'X'; printf("Name = %s\n",sturec.name); printf("ID = %d\n",sturec.id); printf("Marks="%d\n",sturec.details.marks); printf("Marks=%c\n",sturec.details.grade); return 0; } Output: Name = NoorulHaq ID = 123456 Marks=88 Grade=X 40 Imperative Languages

  41. Arithmetic Expressions Arithmetic expression consists of operators, operands, parenthesis and function calls Operator precedence rules: Describes how the expression is evaluated if adjacent operators are of different precedence levels Typical precedence levels follow higher to lower precedence in this order: Parenthesis, unary operators, **, * or / , + or - Operator associativity rules: Describes how the expression is evaluated if adjacent operators are of same precedence level Typical associativity rules are: left to right except for ** which is right to left 41 Imperative Languages

  42. Arithmetic Expressions Order of operator evaluation: Typical evaluation order is: values of the variables are fetched; constant values are fetched either from memory or from the instruction itself; evaluate all operands and operators first, with priority for parenthesized expressions; functional references are handled. Functional site effects: Function might change the value of a two-way parameter or a non-local variable To prevent these side effects, we do either of the following: Disallow functional side effects by having no two-way parameters and no non-local variables in functions. This will work but will prevent the flexibility of programming. Write language definition that the operand evaluation order be fixed. This might create some impact on the compiler optimizations. 42 Imperative Languages

  43. Arithmetic Expressions Usage of user-defined operator overloading: One operator is used for more than one purpose + operator is used for both int and float types * operator is used for referencing pointers and multiplication in C and C++ Usage of overloaded operations might prevent compiler error detection and reduce readability User-defined overload operators are allowed in C++ and Ada Java does not support user-defined operator overloading. Using mixed modes within expressions: Mixed mode expression is one that has operands of different types. Restrictions on operand evaluation side effects. Arithmetic expressions could use unary operator that has one operand or binary operator that has two operands or ternary operator that has three operands. 43 Imperative Languages

  44. Type Conversions Narrowing Conversion Converts an object to a type that cannot include all of the values of the original type Example: float to int Widening Conversion Converts an object to a type that can include at least approximations to all the values of the original type Example: int to float In mixed mode expressions, all numeric types are coerced in expressions using widening conversions FORTRAN allows mixed mode arithmetic with real and integer data but Pascal does not allow; Ada virtually has no coercions in expressions In C#, widening conversion is implicit; Java allows widening conversion using type casting Explicit type conversions are called as casts (type casting) Example in Java: (int) speed /* Initially, speed is of float type */ 44 Imperative Languages

  45. Relational and Boolean Expressions C, C++, and Java have over 40 operators and at least 15 different levels of precedence C does not have boolean data type 45 Imperative Languages

  46. Input/Output FORTRAN uses READ and WRITE statements. There is no Stream I/O. READ(5,10) A,I 10 FORMAT(F5.3, I3) Where 5 in READ statement indicates the input device and 10 in READ statement indicates the FORMAT statement F & I indicates the format of input as floating point and integer respectively. The numbers 5.3 and 3 in FORMAT statement indicate the number of digits used for representation. Concept of Stream I/O is used in Pascal. Input is viewed as a long stream of values that can occur virtually anywhere in terms of spacing. Main concept of input is that it be ordered so that the appropriate values show up for assignment to the corresponding input variables 46 Imperative Languages

  47. Short-Circuit Evaluation While evaluating an expression, evaluation is stopped in the middle as further evaluation won't change the value of the expression Ada programmers should choose one style (either full evaluation or short circuit) for an entire program, using the other style (not chosen style) only if necessary Languages like C, C++, C# and Java use short-circuit evaluation for the usual Boolean operators (&& and ||), but also provide bitwise Boolean operators that are not short circuited (& and |). 47 Imperative Languages

  48. Assignment Statement Different symbols are used Fortran, Basic, PL/I, C, C++ and Java use = Algol, Pascal and Ada use := Different assignments: In Perl, multiple target assignments can be done using ($a, $b) = (12, 13); In C, C++, C# and Java, conditional target setting can be done using total = (first == true) ? total : subtotal; If the condition (first == true) is true, then expression1 (total = total) is evaluated. Else, expression2 (total = subtotal) will be evaluated. Compound assignment operators are present in C, C++, C# and Java as sum += next; Unary assignment operators are present in C, C++, C# and Java as b++; 48 Imperative Languages

  49. Control Statements Different types: Selection statements like if, switch. Iterative statements like for, while, foreach. Unconditional branching like goto. Control Flow Composition: Execution is done one statement after another in sequence Statements can be grouped as compound statements begin … end OR { … } Selection or Alternation or Conditional Statements Only one of the given alternatives is evaluated Iteration Repeated few times without any condition Repeat only if the condition is satisfied 49 Imperative Languages

  50. Control Statements Iteration Repeating based on a counter. Counter is incremented or decremented Repeat based on data availability Indefinite repetition: while true Selection Choose between two or more paths Two-way selectors: if … then … else Fortran: IF (boolean_expr) statement The major drawback with this IF statement is that only one statement could be selected. If we need more statements, we need to use GOTO statement. Nested Selectors: if … if … else To which if does the else belongs to. C or Java: else is connected to the nearest if 50 Imperative Languages

More Related