COEN Expressions and Assignment

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.
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.
CPS 506 Comparative Programming Languages Names, Scope, Expression.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
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.
1 CS Programming Languages Class 09 September 21, 2000.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
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.
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
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
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:

COEN 171 - Expressions and Assignment Precedence, associativity, evaluation order, side effects Overloading Type conversions Relational and Boolean expressions Assignment statements

Expressions and Assignment Statements Common in all imperative languages often included in functional languages (SET!) and declarative languages (is) <target location> <assign operator> <source expression> “l value” and “r value” Language design issues operators and precedence associativity order of evaluation evaluating mixed mode expressions short-circuit evaluation of Boolean expressions

Operators and Precedence First issue is operators to provide (and symbols) +, -, *, / pretty much universal div, mod, ** fairly common Pascal has no ** <, >, =, /=, etc., and, or, not pretty much universal special operators appropriate for language purpose APL array manipulation (transpose, invert, etc.) SNOBOL pattern matching operations ++, +=, etc. in C

Operators and Precedence (continued) Then determine precedence among operators determines which operations are done first typical is exponentiation multiplicative additive relational logical APL has all operators at same level Parentheses can always be used to override operator precedence

Associativity Determines order in which consecutive operators of equal precedence are evaluated left to right (left associativity) A + B + C means (A + B) + C right to left (right associativity) A + B + C means A + (B + C) Most operators in most languages are left associative Fortran **; C ++, --, unary +- are right associative ALL operators in APL are right associative Ada ** is non-associative programmer must explicitly use parentheses to determine order of operations

Side Effects and Evaluation Order Operand evaluation order variables just fetch the value constants sometimes a fetch from memory sometimes the constant is in the machine language instruction parenthesized expressions evaluate all operands and operators first function references the case of most interest! order of evaluation is crucial

Side Effects and Evaluation Order (cont.) A side effect is when a function or operator does more than just return a value change a parameter or global variable usually considered a bad programming practice Ada disallows changes to function parameters If side effects are possible, then the order in which an operator’s operands are evaluated can have an impact on the result in C x = 3; z = x * ++x; /* 12 or 16?*/ x = 3; a[x] = x++; /* assign 3 to a[3] or a[4]? */ evaluation order by itself can have impact for expression A + B + C order of additions shouldn’t matter but if A and C have very large positive values, and B has very large negative value, but order is A + C + B, error a = 10; b = a + fun(&a);

Side Effects and Evaluation Order (cont.) One solution: write the language definition to disallow functional side effects no two-way parameters in functions Ada no nonlocal references in functions FORTRAN advantage it works! disadvantage programmers want the flexibility of two-way parameters and nonlocal references Another solution: write the language definition to demand that operand evaluation order be fixed limits some compiler optimizations

Operator Overloading Overloading means using the same name to represent two or more different things + means both integer and real addition in most languages also applies to more than operators (functions, procedures) Most modern languages (Ada, C++, Fortran 90) allow user-defined operator overloading Ada function “+” (left, right: MYTYPE) returns MYTYPE is then can use + as an infix operator for things of appropriate type type MYTYPE is ...; A, B, C: MYTYPE; C := A + B; compiler determines which + to use based on types of operands

Problems With Operator Overloading Loss of compiler error detection omission of an operand should be a detectable error * in C and C++ avoid by introduction of new symbols Pascal’s div C++ and Ada allow user-defined overloaded operators users can define nonsense operations readability may suffer

Type Conversions Often want to write expressions that are mixed mode (contain operands of more than one type) real + integer This implies a need to convert one type to another so the expression can be evaluated coercion is implicit conversion, done automatically by the compiler implies semantics that define rules for determining type to convert to from operands problem is loss of error detection casts are explicit conversions specified by the programmer can lead to very clumsy expressions if doing a lot of mixed mode expressions Ada: float (Index); C: (int) speed;

Type Conversions (continued) Whether implicit or explicit, conversions can be widening convert to a type with a greater representation range although perhaps with a loss of precision integer to real narrowing convert to a type with a more restricted range double precision to real promoting convert to a type with additional semantic information integer to character (Pascal chr function) demoting strip away semantic information character to integer (Ada val attribute)

Type Conversions (continued) Most languages provide some coercions and allow casts PL/I allows coercion between almost any types DCL A, B, C INT; if (A <= B <= C) then ... A <= B yields a Boolean value, which is a single bit 0 or 1 convert bit string to integer to compare to C bit <= C is true for any positive values of C Ada allows no coercion all conversions must be casts conversions are allowed between all numeric types other conversions only allowed between derived types that share an ancestor type foo is new Boolean; type bar is new Boolean; A: foo; B: bar; A := foo(b);

Relational and Boolean Expressions Relational expression consists of 2 operands (which may themselves be expressions) and a relational operator <, <=, >, >=, /=, in result is a Boolean value, which is usually implemented as a 0 or non-zero integer value Boolean expression consists of Boolean variables, relational expressions, and Boolean operators and, or, not, xor result is a Boolean value Design issue for languages is whether to implement lazy or strict evaluation of Boolean expressions strict evaluation evaluates both operands, then gets result lazy (or short-circuit) evaluation stops evaluating as soon as truth or falsehood of expression can be determined

Relational and Boolean Expressions (continued) Suppose A is a 100-element array, then while (scan < 101) and (A[scan] <> key) works with short-circuit evaluation runtime error with strict evaluation Most languages perform strict evaluation C, C++ and Java perform short-circuit evaluation Ada provides operators to allow programmer to control which occurs and, or are strict evaluation and then, or else are lazy evaluation C oddity A < B < C is legal so is A == B == C

Assignment Fairly standard in all imperative languages major design choice is whether assignment functions as a statement or a function operator used to indicate assignment varies disambiguate assign and equality PL/I A = B = C assigns to A the result of B = C as comparison what is A = B = C in C As a statement, usually require a reference (location) and a value usually single target for assignment PL/I allows multiple A, B = 200; As a function, returns a value APL, C, C++

Assignment (continued) C and C++ provide a rich diversity of assignment operators and choices Conditional targets in C++ and Java (X > Y) ? A : B = 24.3; A gets value if X > Y, else B does Conditional sources AVG = (COUNT == 0) ? 0 : SUM / COUNT; if COUNT is 0, AVG gets 0, else AVG gets SUM / COUNT Compound assignment operators total += value; /* total = total + value */ also in Algol 68

Assignment (continued) Unary assignment operators count++; /* count = count + 1 */ sum = ++count; /* count = count + 1; sum = count */ right associative - count ++ /* - (count ++) */ Assignment as operator while ( (ch = getchar() ) != EOF ) { read input, assign to ch; while compares to EOF this is another example of an expression side effect means less error checking at compile time suppose meant while ( (ch == getchar() )

Mixed-mode Assignment FORTRAN, C, C++ Any numeric value can be assigned to any numeric scalar variable, all necessary coercions are done Pascal Integers assigned to reals, not other way Java Only widening conversions allowed Ada No coercion