Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,

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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
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.
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.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
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.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
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 An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
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 creating information

topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic, relational, boolean, assignment

Arithmetic  +, -, *, /, sometimes **  fully parenthesized expressions easy ((3*5)+((6/2)-(7+1)))  associativity and precedence for user’s convenience 3*5 + 6/2 -(7+1)  BUT no implicit operations: 2ab 3

Precedence **FORTRAN++ -- C++, java * /+ - unary + -* / % + - binary no precedence APL fully parenthesized pure LISP

Associativity (at precedence level)  left to right most common –  8  right to left rare 2 ** 2 ** 3  256 ( ** FORTRAN) 3 X  18 ( no precedence APL )  no associativity ( ** Ada) 2 ** 2 ** 3  error (2 ** 2) ** 3  64 OR2 ** (2 ** 3)  256

The trinary expression  op1 ? op2 : op3 c, c++, java (3 + 4 < 10) ? 12 – 4 : 40 * 40  8  why () on op1? first expression is boolean

Operands  Side effects  Type conversions

Operands - side effects  Side effect - procedure changes a data value of a global variable or parameter var int d,g,h; begin function f(int v) d := 30; begin g := 20; v := 100; h := 10; g := 200; d := f(h) f := 300end. end; d is 300, h is 100, g is 200 {parameter v passed by reference}

Side effects and order of operand evaluation  side effects make order of operand evaluation important var int d,g,h; begin function f(int v) d := 30; begin g := 20; v := 100; h := 10; g := 200; d := g + f(h) f := 300end. end;Is d = 320 or 500?

Type conversion  what determines type of expression result? 1. operator if not overloaded: e.g. Pascal 3 div 4 is 0 but 3 / 4 is operands if operator is overloaded: e.g. in java 3/4 is 0 but 3.0/4 is 0.75 coercion: implicit translation of value to another type standard type conversion rule - coercion to a ‘wider’ type is OK

Relational expressions  same precedence, no associativity operands are ‘numeric’ and result is boolean = or ==>>=.EQ..GT..GE. != or <><<=.NE. or /=.LT..LE.

Relational expressions  same precedence, no associativity operands are ‘numeric’ and result is boolean EXCEPT c – uses 0 for false, ~0 for true so result (1) is int and can be compared: 6 > 5 > 4  0 (false) why? 6 > 5  1 (true) then 1 > 4  0 (false)

Logical or boolean  boolean operands and result  operators unary (not) and binary (and, or,...) FORTRANAdac,c++,java.AND.andand then&&&.OR.oror else||| xor SHORT-CIRCUIT EVALUATION

Short Circuit Evaluation  stop evaluation if value of expression known (a * b) * ( 1 + b/a) stop if b==0? if a==0?  not used in arithmetic  CAN be used in logical expressions

Short Circuit Evaluation of Boolean expressions  op1 && op2 if op1 is false, expression value is false  op1 || op2 if op1 is true, expression value is true

Short Circuit example int i=0; while ( i<arr.length && arr[i]!= k ) i++; if k is not found, either short circuit or range error && and || short circuit in c, c++, java

Assignment 1.high level ‘store’ 2.binary operator 3.type matching of operands 4.return a value? i.e. assignment statement or assignment expression?

Assignment Target (left side)  memory cell (not data value like most operands)  type matching – strongly typed or typeless determines coercion of right side or left side  dynamic binding of cell to expression

Type matching (mixed mode)  typeless language a = 3.4 * y; // a becomes float a = “tttt”; // a becomes string  strongly type-checked language double a = 4 * 5 ; // int 20 coerced to double int a = 4.1 * 3.2; // type mismatch error

Assignment Target Binding static: x = 3 * y + 5; dynamic: a[i-4] = 3 * y + 5; ((Point)pt).xVal = 3 * y + 5; odd ? oddVale : evenVal = 3 * y + 5;

Assignment Orthogonality c, c++, java – operators combining arithmetic and assignment: not orthogonal unary ++: count++ binary +=: sum += count

Multiple target assignment  PL/1: multiple parallel targets A, B, C, D = 100;  c, c++, java: assignment expression returns value A = B = C = D = 100; right to left associativity

The assignment/expression mess in the legacy of c  goal: efficiency count++; sum += x; a = b = c = 0;  consequence: nested expressions and assignments – readability? a = b + (c = d / b++) – 1; (Sebesta, p.303)

The assignment/expression mess in the legacy of c a = b + (c = d / b++) – 1; (Sebesta, p.303) 24 1, ?