expressions creating information. topics operators: precedence, associativity, parentheses,...

24
Expressions creating information

Upload: jeremy-knight

Post on 27-Dec-2015

244 views

Category:

Documents


0 download

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 convenience3*5 + 6/2 -(7+1)

BUT no implicit operations: 2ab3

Precedence

** FORTRAN ++ -- C++, java

* / + - unary

+ - * / %

+ - binary

no precedence APL

fully parenthesized pure LISP

Associativity (at precedence level)

left to right most common3 + 4 – 5 + 6 8

right to left rare2 ** 2 ** 3 256 ( ** FORTRAN)3 X 4 + 2 18 ( no precedence APL )

no associativity ( ** Ada)2 ** 2 ** 3 error(2 ** 2) ** 3 64

OR 2 ** (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; beginfunction f(int v) d := 30; begin g := 20; v := 100; h := 10; g := 200; d := f(h) f := 300 end. 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; beginfunction f(int v) d := 30; begin g := 20; v := 100; h := 10; g := 200; d := g + f(h) f := 300 end. end; Is d = 320 or 500?

Type conversion

what determines type of expression result?

1. operator if not overloaded: e.g. Pascal3 div 4 is 0 but 3 / 4 is 0.75

2. operands if operator is overloaded: e.g. in java3/4 is 0 but 3.0/4 is 0.75

coercion: implicit translation of value to another typestandard type conversion rule - coercion to a ‘wider’

type is OK

Relational expressions

same precedence, no associativityoperands are ‘numeric’ and result is

boolean= or == > >=.EQ. .GT. .GE.

!= or <> < <=.NE. or /= .LT. .LE.

Relational expressions

same precedence, no associativityoperands are ‘numeric’ and result is

booleanEXCEPT c – uses 0 for false, ~0 for trueso 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, ...)FORTRAN Ada c,c++,java

.AND. and and then && &

.OR. or or 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 operator3. type matching of operands4. 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 languagea = 3.4 * y; // a becomes float

a = “tttt”; // a becomes string

strongly type-checked languagedouble 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 targetsA, 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,35 67

?