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

Slides:



Advertisements
Similar presentations
ISBN Chapter 7 Expressions and Assignment Statements.
Advertisements

CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions.
ICE1341 Programming Languages Spring 2005 Lecture #12 Lecture #12 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
ISBN Chapter 7 Expressions and Assignment Statements.
COEN Expressions and Assignment
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
CS2403 Programming Languages Expressions and Assignment Statements Chung-Ta King Department of Computer Science National Tsing Hua University (Slides are.
CSE 452: Programming Languages Expressions and Control Flow.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Expressions, Evaluation and Assignments
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
C H A P T E R S E V E N Expressions and Assignment Statements.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
1 Chapter 7 Expressions and Assignment statements.
C HAPTER 7 Expressions and Assignment Statements.
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 Expressions and Assignment Statements. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
April 2, ICE 1341 – Programming Languages (Lecture #12) In-Young Ko Programming Languages (ICE 1341) Lecture #12 Programming Languages (ICE 1341)
1 CS Programming Languages Class 09 September 21, 2000.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
1 CS Programming Languages Class 10 September 26, 2000.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignments Statements.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
CS2403 Programming Languages Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
Expressions and Assignment Statements
College of Computer Science and Engineering
Expressions and Assignment Statements
Expression and Asignment Statements
Chapter 7 Expressions and Assignment Statements.
Chapter 7: Expressions and Assignment Statements Sangho Ha
B=15 b = a + fun(a); a=10 fun(a)=5.
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Expressions and Assignment Statements
Presentation transcript:

March 31, 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

March 31, 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

March 31, 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

March 31, 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) obj.d)

March 31, 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) obj.d)

March 31, 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

March 31, 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

March 31, 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) obj.d)

March 31, 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

March 31, 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 ) r = 3 / 5 r := 3 div 5

March 31, 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

March 31, 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)

March 31, 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;

March 31, 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)

March 31, 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