Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

COP4020 Programming Languages Expression and assignment Prof. Xin Yuan.
ISBN Chapter 7 Expressions and Assignment Statements.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions.
And other languages….  puts “yes” // global, method of Kernel  Math.sqrt 2 // object Math, method sqrt  message.length // object message, method length,
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
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.
Louden, Chapter 7 - Control I: Expressions and Statements Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
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.
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
Chapter 4: Basic C Operators
ISBN Chapter 7 Expressions and Assignment Statements.
1 Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
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.
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.
1 Chapter 7 Expressions and Assignment statements.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Semantics (1).
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.
ISBN Chapter 7 Expressions and Assignment Statements.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
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
Expressions and Assignment Statements
Expressions and Assignment
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 4: Expression and Operator
Chapter 7: Expressions and Assignment Statements Sangho Ha
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Expressions and Assignment Statements
Presentation transcript:

Expressions (chapter 7 of textbook) Semantics of expressions depends on: –operator evaluation order operator precedence operator associativity –operand evaluation order –side effects –short-circuit evaluation

Arithmetic expressions expressions involving arithmetic operators such as +, -, * and / –most are binary only (taking two arguments) –some are unary (taking one argument) most languages write binary arithmetic operators infix (between their operands), as in mathematics: x + y some don’t (e.g. Scheme): (+ x y) Scheme’s operators are of arbitrary arity: ( )  (+ (+ (+ 1 2) 3) 4)  10 ( )  (- (- (- 1 2) 3) 4)  -8 (* )  (* (* (* 1 2) 3) 4)  24 (/ )  (/ (/ (/ 1 2) 3) 4)  1/24

Precedence Without parenthesization, in what order are different operators evaluated? E.g. what is the value of * 3? Is it 9 or 7? Most languages adopt precedence rules of mathematics (e.g. * and / have higher precedence than + and -). Unary operators typically have higher precedence than binary ones (e.g. unary – has higher precedence than binary -). In C-like languages, postfix ++ and -- have higher precedence than unary -. Suppose x has value 2, then -x++ has value -2 (but leaves x with value 3).

Associativity Determines whether unparenthesized expressions involving operators of the same precedence group to the left or to the right: 5 – 3 – 4  (5 – 3) – 4  –2 (left associativity) 5 – 3 – 4  5 – (3 – 4)  6 (right associativity) In most languages +,-,*,/ are left associative. In Scheme the question of associativity does not come up (since all expressions must be fully parenthesized)

Associative operators Some operators are, mathematically speaking, associative. For example, + is mathematically associative, meaning that x+y = y+x. For example, - is not: x-y != y-x (unless x and y are the same). Compilers can take advantage of this to reorder expressions for increased execution speed, but… Operators like + are not generally associative in programming languages due to the limited precision with which numbers are represented. With floating point operations + and * are not associative: … != …

Conditional expressions A conditional expression has a value. In C-like languages the if-statement is not an expression, and has no value: if (count==0) { average = 0; } else { average = sum/count; }

Conditional expressions in C-like languages In C-like languages we cannot write: average = if (count==0) 0; else sum/count; In C-like languages we must write: average = (count==0) ? 0 : sum/count; In functional languages conditionals are expressions: val average = if (count=0)then 0 else sum/count; Orthogonality in language design favors NOT having different syntax for conditional statements and conditional expressions.

Implications of conditional expressions A conditional expression requires that all branches of the conditional: –have a value –have the same type An if-then without an else is semantically incoherent as an expression.

Operand evaluation order In an expression like +, is or evaluated first? Without side-effects or short-circuit evaluation, order is irrelevant. Aside: if an expression has the same value no matter what its context of evaluation, then it is termed referentially transparent.

Side-effects & operand evaluation If expressions have side effects (e.g. mutate variables) then order of evaluation becomes very important. Operators like ++ are side-effecting: ++x is equivalent to x = x + 1.

Short-circuit evaluation and operand evaluation If expression evaluation is short-circuited then order of evaluation also becomes very important. In C-like languages && is typically short- circuiting.

Operator overloading Positives: can make code more clear –in C++ ‘<<’ can be overloaded for different types to handle stream output –in C++ ‘+’ can be overloaded to provide addition of novel types Negatives: can make code less clear –in C ‘&’ is overloaded with unrelated meanings –nothing stops a user from overloading ‘+’ to do something other than addition

unary/binary overloading - is overloaded with unary and binary meanings in most languages In ML - is binary subtraction only, and ~ is unary negation. –Is benefit worth the trouble?

Type coercions and type checking Type coercions weaken type checking.