Expressions, Evaluation and Assignments

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.
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 & 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.
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.
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.
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 & Assignments
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:

Expressions, Evaluation and Assignments Arithmetic expressions Overloaded operators Type conversions Relational and Boolean expressions Short-circuit evaluation Assignment statements Mixed-mode assignment statements Sebesta Chapter 7

Expressions Expressions are fundamental means of specifying computations in programming languages Understanding how expressions are evaluated requires knowing the order in which operator and operand are evaluated Essence of imperative languages is the dominant role of assignment statements, including expressions

Arithmetic Expressions Evaluation of numeric expressions Motivation for the development of PLs Remember trajectory tables? Arithmetic expressions consist of Operators Operands Parentheses/delimiters Function calls

Design Issues for Arithmetic Expressions What are the operator precedence rules? What are the operator associativity rules? What is the order of operand evaluation? Are there restrictions on operand evaluation side effects? Is user-defined operator overloading supported? What mode mixing in expressions is allowed?

Arity of Arithmetic Expressions Number of operands/arguments of a function A unary operator has one operand A binary operator has two operands Most common operators A ternary operator has three operands Some languages support N-ary operators In Lisp, a benefit of prefix representation (* pi r r) vs. pi*r*r or pi*r^2

Operator Precedence Rules Precedence define the order in which adjacent operators are evaluated Adjacent - separated by at most one operand Different PLs have different precedence levels Typical precedence levels – highest to lowest Parentheses Unary operators ** (exponentiation, if the language supports it) *, /, % (modulo) +, -

Operator Associativity Rules Define the order in which adjacent operators with the same precedence level are evaluated Typical associativity rules Left to right, except ** which is right to left Unary operators may associate right to left (e.g., FORTRAN) APL is different All operators have equal precedence and All operators associate right to left! Parentheses override precedence and associativity rule

Expression Evaluation Process Order of evaluation is crucial Variables fetch value from memory Constants either implicit in instruction or fetch from memory Parenthesized expressions evaluate all operands and operators first Function references the most interesting

Functions/Procedures Parameters – pass by value (in) or by reference (in/out) Return value Input/Output Side Effects Function/Procedure Arguments/ Parameters Result/ Return value Input/Output Side Effects

Side Effects Side effect a function or procedure changes a two-way parameter or a non-local variable A major problem with side effects: When a function referenced in an expression alters another operand of the expression; e.g., for a parameter change: a = 10; b = a + fun(&a); /*Assume fun changes its parameter*/ Results of the expression depend on the order of evaluation of statements!! why is this bad?

Solution 1: Prohibit Side Effects! Language definition prohibits side effects No two-way parameters No non-local references Advantage It works! E.g. functional languages Disadvantages: Need flexibility of two-way parameters and non-local variables What about C? What about Java? Copying of parameters to avoid side effects

Solution 2: Fix Evaluation Order Operand evaluation order is fixed in language definition Advantage We always know how expression will be evaluated Disadvantage This limits some compiler optimizations

Conditional Expressions Ternary operator <cond> ? <expr1> : <expr2> Same as if (<cond> ) <expr1> else <expr2> C, C++, and Java <condition> average (count == 0) ? 0 : sum / count Lisp: (if <test> <do-if-true> <do-ifnot>) Short-circuit evaluation means Evaluate test first and then Evaluate only the branch taken e.g. avoid division by zero above

Overloading Operators Operator overloading use of an operator for more than one purpose Some are common (e.g., + for int and float) Some are potential trouble e.g., * in C and C++, / for int and float in Java Loss of compiler error detection Missing operand should be a detectable error Some loss of readability Can be avoided by introduction of new symbols e.g., Pascal’s div

User-defined Overloaded Operators C++ and Ada allow user-defined overloaded operators Problems Users can define nonsense operations Readability may suffer, even when the operators make sense

Type Conversions Narrowing conversion converts to a “smaller” type (type has fewer values) e.g., float to int 3.99 to 4 Widening conversion converts to a type that includes all values of the original type or at least an approximation of each e.g., int to float 4 to 4.0f

Type Conversions Mixed-mode expression Coercion Disadvantage Operands of different types Coercion An implicit type conversion Disadvantage Decreases the type error detection ability of the compiler In most languages, widening conversions of numeric types in expressions can be coerced In Ada, there are virtually no coercions in expressions

Explicit Type Conversions In C, C++, Ada, Java called casts E.g., Ada: FLOAT (INDEX) --INDEX is INTEGER type converts to floating point E.g., Java: float speed = 45.5; (int) speed; /* =45; cuts off fractional part*/

Errors in Expressions Inherent properties of mathematical functions e.g. division by zero, infinity Approximate representations Fractions (e.g. 2/3, 0.1) and irrational numbers like π and e Approximate huge integers with floating point Limitations of computer arithmetic e.g. overflow, underflow If ignored by the run-time system (may even be undetectable) can lead to crashes, erroneous output, unpredictable behavior Less of a problem in some languages! E.g. exact fractions and huge integers in Lisp prevent errors of type 2 & 3

Relational Operators, Boolean Expressions Boolean data type 2 values True False Boolean expression Has relational operators and operands of various types Evaluates to a Boolean value Operator symbols vary among languages e.g.not equal != /= .NE. <> #

Boolean Expressions Operands are Boolean Result is Boolean Boolean operator comparison F77 FORTRAN 90 C Ada Lisp .AND. and && .OR. or || .NOT. not ! xor

Odd Boolean Expressions in C C (until very recently) had no Boolean type used int 0 for false, and 1 or nonzero for true One odd characteristic of C’s expressions: x < y < z Is a legal expression, but the result is not what you might expect! - I.e.(x<y)&(y<z) What does it do? Hint: C is left associative, what is z compared to?

Operators Precedence Precedence of Ada operators: **, abs, not *, /, mod, rem [unary] -, + [binary] +, -, & [relative] in, not in and, or, xor, then, or, else C, C++, and Java have over 40 operators, and at least 15 different levels of precedence

Short Circuit Evaluation Suppose Java did not use short-circuit evaluation Problem table look-up for (i = 1; i < a.length) && (a [i] != x); i++) {} Problem: reading from a file until eof Short-circuit evaluation has the problem of side effects e.g. (a > b) || (b++ / 3) vs. a > b) || (++b / 3)

Short Circuit Evaluation in PLs C, C++, Java Provide short-circuit Boolean operators && and|| As well as operators that are not short circuit: & and| why both? Ada More operators, programmer can specify either Not short circuit using and, or Short-circuit using and then, or else FORTRAN 77 short circuit, any side-affected variables must be set to undefined

Assignment Statements Assignment operator syntax = FORTRAN, BASIC, PL/I, C, C++, Java := ALGOLs, Pascal, Ada setf/setq in Lisp Very bad if assignment = overloaded as relational = e.g. in PL/I: A = B = C; Note difference from C’s == A common C error using = when it should be ==

Complex Assignment Statements Multiple targets (PL/I) A, B = 10 Compound assignment operators in C, C++, Java sum += next; Conditional targets in C, C++, Java (first == true) ? total : subtotal = 0 Unary assignment operators in C, C++, Java a++; C, C++, and Java treat = as an arithmetic binary operator a = b * (c = d * 2 + 1) + 1

Assignment Statement as an Expression In C, C++, Java Assignment statements produce results So, they can be used as operands in expressions while ((ch = getchar()) != EOF){…} Disadvantages Another kind of expression side effect Readability

Mixed-Mode Assignment FORTRAN, C, C++ any numeric value can be assigned to any numeric variable conversion is automatic Pascal integers can be assigned to reals, but reals cannot be assigned to integers must specify truncate or round Java only widening assignment coercions are done Ada no assignment coercion Lecture-specific question: Advantages/disadvantages of these approaches?