Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 31, 2004 1 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)

Similar presentations


Presentation on theme: "March 31, 2004 1 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)"— Presentation transcript:

1 March 31, 2004 1 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341) Lecture #11 March 31, 2004 In-Young Ko iko.AT. icu.ac.kr Information and Communications University (ICU) iko.AT. icu.ac.kr

2 March 31, 2004 2 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Announcements Any questions on the project #1? Any questions on the project #1? The midterm exam will be on: The midterm exam will be on: April 9 th, 1:00PM-3:00PM The midterm exam will cover: The midterm exam will cover: Chapter 1 Chapter 1 Chapter 3 (except 3.5.3) Chapter 3 (except 3.5.3) Chapter 5 Chapter 5 Chapter 6 (except 6.9.9) Chapter 6 (except 6.9.9) Chapter 7 Chapter 7

3 March 31, 2004 3 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Review of the Previous Lecture Associative Arrays Associative Arrays Record Types Record Types Union Types Union Types Pointer Types Pointer Types

4 March 31, 2004 4 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls Arithmetic expressions consist of operators, operands, parentheses, and function calls Unary Operators Unary Operators e.g., !, ++, --, + (identity operator), - Binary Operators Binary Operators e.g., &&, ||, ==, !=, +, -, *, / Ternary Operators Ternary Operators e.g., expr1 ? expr2 : expr3 a + b * Math.sqrt(c) + (f (obj) - 1.257 + obj.d)

5 March 31, 2004 5 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Design Issues for Arithmetic Expressions What are the operator precedence rules? What are the operator precedence rules? What are the operator associativity rules? What are the operator associativity rules? What is the order of operand evaluation? What is the order of operand evaluation? Are there restrictions on operand evaluation side effects? Are there restrictions on operand evaluation side effects? Does the language allow user-defined operator overloading? Does the language allow user-defined operator overloading? What type mixing is allowed in expressions? What type mixing is allowed in expressions? * AW Lecture Notes a + b * Math.sqrt(c) + (f (obj) - 1.257 + obj.d)

6 March 31, 2004 6 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Operator Precedence Rules Define the order in which adjacent operators of different precedence levels are evaluated Define the order in which adjacent operators of different precedence levels are evaluated Typical precedence levels: Parentheses  Unary operators  **  *, /  +, - Typical precedence levels: Parentheses  Unary operators  **  *, /  +, - a + b * c + ++d – (e + f) (1)(2)(3) (4) (5) (6) * AW Lecture Notes

7 March 31, 2004 7 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Operator Associativity Rules Define the order in which adjacent operators with the same precedence level are evaluated Define the order in which adjacent operators with the same precedence level are evaluated Typical associativity rules: Left to right (except ** in Ada and Fortran) Typical associativity rules: Left to right (except ** in Ada and Fortran) APL; all operators have equal precedence and all operators associate right to left APL; all operators have equal precedence and all operators associate right to left Precedence and associativity rules can be overriden with parentheses (e.g., to improve precision in calculating large numbers) Precedence and associativity rules can be overriden with parentheses (e.g., to improve precision in calculating large numbers) * AW Lecture Notes a + b - c; a ** b ** c; - a - b; - a ** b; - 17 mod 5

8 March 31, 2004 8 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Operand Evaluation Order Variables: just fetch the value from memory Variables: just fetch the value from memory Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction Parenthesized expressions: evaluate all operands and operators first Parenthesized expressions: evaluate all operands and operators first Function references: need to consider side effects Function references: need to consider side effects * AW Lecture Notes a + b * Math.sqrt(c) + (f (obj) - 1.257 + obj.d)

9 March 31, 2004 9 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Side Effects Functional side effects occur when a function changes a two-way parameter or a nonlocal variable Functional side effects occur when a function changes a two-way parameter or a nonlocal variable Possible Solutions: Possible Solutions: Allow no two-way parameters and non-local references in functions (  impossible to return multiple values from a function, inefficiency in parameter passing) Allow no two-way parameters and non-local references in functions (  impossible to return multiple values from a function, inefficiency in parameter passing) Fix the operand evaluation order (  limits compiler optimizations) Fix the operand evaluation order (  limits compiler optimizations) int main() { int a = 10; int b = a + triple(&a); } int triple(int *val) { (*val) *= 3; return *val; } * AW Lecture Notes

10 March 31, 2004 10 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Overloaded Operators Operator Overloading: use of an operator for more than one purpose Operator Overloading: use of an operator for more than one purpose Need to consider readability and reliability of an overloaded operator Need to consider readability and reliability of an overloaded operator C++ and Ada allow user-defined overloaded operators C++ and Ada allow user-defined overloaded operators e.g., int a[10], b[]; b = a + 5; //??? b = a + 5; //??? int a, *b, c; float d; r = a * *b + c – (d + 1.345) r = 3 / 5 r := 3 div 5

11 March 31, 2004 11 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Type Conversions Narrowing Conversion: converts an object to a type that cannot include all of the values of the original type e.g., float to int Narrowing Conversion: converts an object to a type that cannot include all of the values of the original type e.g., float to int Widening Conversion : converts an object to a type that can include at least approximations to all of the values of the original type e.g., int to float Widening Conversion : converts an object to a type that can include at least approximations to all of the values of the original type e.g., int to float Mixed-mode Expression: has operands of different types Mixed-mode Expression: has operands of different types Coercion: an implicit type conversion e.g., int num = 3.14; Coercion: an implicit type conversion e.g., int num = 3.14; Ada provides virtually no coercions in expressions Ada provides virtually no coercions in expressions Type Casting: an explicit type conversion e.g., int num = (int)3.14; Float(sum) -- as a function call Type Casting: an explicit type conversion e.g., int num = (int)3.14; Float(sum) -- as a function call * AW Lecture Notes

12 March 31, 2004 12 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Relational and Boolean Expressions Relational Operator: an operator that compares the values of two operands and returns a Boolean value Relational Operator: an operator that compares the values of two operands and returns a Boolean value e.g., !=, /=,.NE., <> e.g., !=, /=,.NE., <> Boolean Operator: an operator which operands are Boolean and its result is Boolean Boolean Operator: an operator which operands are Boolean and its result is Boolean e.g.,.AND.,.OR.,.NOT., &&, ||, ! e.g.,.AND.,.OR.,.NOT., &&, ||, ! C has no Boolean type – it uses int type with 0 for false and nonzero for true C has no Boolean type – it uses int type with 0 for false and nonzero for true e.g., a > b > c // ??? * AW Lecture Notes !(a != b && c > d || e)

13 March 31, 2004 13 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Short-Circuit Evaluation Short-Circuit Expression: an expression which result is determined without evaluating all of the operands and operators Short-Circuit Expression: an expression which result is determined without evaluating all of the operands and operators A short-circuit expression may prevent side effects in the expression A short-circuit expression may prevent side effects in the expression e.g., (a > b || ((b++) / 3) Ada allows the programmers to specify short- circuit evaluation (and then, or else) Ada allows the programmers to specify short- circuit evaluation (and then, or else) (13 * a) * (b / 13 – 1) (a >= 0) && (b = 0) && (b < 10) if (i < len && a[i] != 0) a[i] *= 2;

14 March 31, 2004 14 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Assignment Statements Simple Assignment (e.g., a = 10, Pascal A := 10) Simple Assignment (e.g., a = 10, Pascal A := 10) Multiple Targets (e.g., PL/I A, B = 10) Multiple Targets (e.g., PL/I A, B = 10) Conditional Targets Conditional Targets (e.g., C-based (first==true)? total : subtotal = 0) Compound Assignment Operators Compound Assignment Operators (e.g., C-based sum += next;) Unary Assignment Operators (e.g., C-based a++;) Unary Assignment Operators (e.g., C-based a++;) Assignment as an Expression Assignment as an Expression (e.g., C while ((ch=getchar()) != EOF) { … } sum = count = 0; // multiple targets) sum = count = 0; // multiple targets)

15 March 31, 2004 15 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Mixed-mode Assignment In FORTRAN, C, and C++, any numeric value can be assigned to any numeric scalar variable In FORTRAN, C, and C++, any numeric value can be assigned to any numeric scalar variable e.g., float val = 12; int num = 12.35; In Java and C#, only widening assignment coercions are allowed In Java and C#, only widening assignment coercions are allowed e.g., float val = 12; int num = (int)12.35; In Ada, there is no assignment coercion In Ada, there is no assignment coercion


Download ppt "March 31, 2004 1 ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)"

Similar presentations


Ads by Google