Download presentation
Presentation is loading. Please wait.
Published byJeremy Knight Modified over 9 years ago
1
Expressions creating information
2
topics operators: precedence, associativity, parentheses, overloading operands: side-effects, coercion arithmetic, relational, boolean, assignment
3
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
4
Precedence **FORTRAN++ -- C++, java * /+ - unary + -* / % + - binary no precedence APL fully parenthesized pure LISP
5
Associativity (at precedence level) left to right most common 3 + 4 – 5 + 6 8 right to left rare 2 ** 2 ** 3 256 ( ** FORTRAN) 3 X 4 + 2 18 ( no precedence APL ) no associativity ( ** Ada) 2 ** 2 ** 3 error (2 ** 2) ** 3 64 OR2 ** (2 ** 3) 256
6
The trinary expression op1 ? op2 : op3 c, c++, java (3 + 4 < 10) ? 12 – 4 : 40 * 40 8 why () on op1? first expression is boolean
7
Operands Side effects Type conversions
8
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}
9
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?
10
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 0.75 2. 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
11
Relational expressions same precedence, no associativity operands are ‘numeric’ and result is boolean = or ==>>=.EQ..GT..GE. != or <><<=.NE. or /=.LT..LE.
12
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)
13
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
14
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
15
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
16
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
17
Assignment 1.high level ‘store’ 2.binary operator 3.type matching of operands 4.return a value? i.e. assignment statement or assignment expression?
18
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
19
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
20
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;
21
Assignment Orthogonality c, c++, java – operators combining arithmetic and assignment: not orthogonal unary ++: count++ binary +=: sum += count
22
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
23
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)
24
The assignment/expression mess in the legacy of c a = b + (c = d / b++) – 1; (Sebesta, p.303) 24 1,3 5 67 ?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.