1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

40
1 Chapter 7 Expressions and assignment statements Robert W. Sebesta

Transcript of 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

Page 1: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

1

Chapter 7 Expressions and assignment statements

Robert W. Sebesta

Page 2: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

2

Major points Arithmetic expressions Overloaded operators Type conversions Relational and boolean

expressions Short-circuit evaluation Assignment statements

Page 3: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

3

Introduction Expressions are the fundamental means

of specifying computations in a programming language

To understand expression evaluation, need to be familiar with the orders of operator( 運算子 ) and operand( 運算元 ) evaluation

Page 4: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

4

Arithmetic expressions Consist of operators( 運算子 ), operands( 運

算元 ), parentheses, and function calls Operators can be

Unary - A unary operator has one operand Binary - A binary operator has two operands,

usually infix Ternary - A ternary operator has three

operands

infix notation 中置表示法;插入表示法根據運算子優先規則和使用成對的限定號(如小括號)來形成數學運算式的一種方法,按這種表示法,運算子插在運算對象中間,每個運算子指出對它兩邊相鄰的運算對象或中間結果所應執行的運算

Page 5: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

5

Arithmetic expressions Design issues

What are operator precedence rules (Operator Evaluation Order)

What are operator associativity rules( 組合規則 )

What is the order of operand evaluation

Are operand evaluation side effects restricted

Can operators be overloaded Is mode mixing allowed

Page 6: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

6

Operator precedence Example : a + b * c

a=3, b=4, c=5;

If evaluated left to right, (3+4)*5=35If evaluated right to left, 3+(4*5)=23

Page 7: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

7

The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated

“adjacent” means they are separated by at most one operand

Typical precedence levels ( 優先次序 )1. Parentheses ( 括號 )2. unary operators ( 單元運算子 )3. ** ( 指數, if the language supports it)4. *, / ( 乘除 )5. +, - ( 加減 )

Operator precedence

Page 8: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

8

Operator precedence Operator precedence in some languages:

FORTRAN***, /all +, -

Pascal*, /, div, modall +, -

C-based PLpostfix( 後置 ) ++,--prefix( 前置 ) ++,--unary +,-*, /, %binary +, -

Ada**, abs*, /, mod, remunary +, -binary +, -

Note :** operator is exponentitation, % operator of C is like the rem operator of Ada, which yields the remainder( 餘數 ) of the first after division by the second

Page 9: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

9

The operator associativity rules for expression evaluation 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 Sometimes unary operators associate right

to left (e.g., FORTRAN) Precedence and associativity rules can be

overriden( 強迫中斷 ) with parentheses

Operator associativity

Page 10: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

10

Operator associativity An operator can be left-, right-, or non-

associative: FORTRAN

Left: *, /, +, - Right: **, e.g., A ** B ** C ≡ A ** (B ** C)

Pascal: all are left-associative C

Left: postfix++, postfix --, %, binary +, binary – Right: prefix ++, prefix --, unary +, unary –

C++ Left: *, /, %, binary +, binary – Right: prefix ++, prefix--, unary -, unary +

Page 11: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

11

Operator associativity Ada

Nonassociative: **, A ** B ** C is illegal A ** (B ** C) or (A ** B) ** C

Consider In Ada : - 17 mod 5 ≡ -(17 mod 5) = -2 In C : - 17 % 5 ≡ (-17 ) % 5 = 3

Page 12: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

12

APL All operators have the same precedence All operators are right to left associative Examples:

A x B x C A x (B x C) A x B + C A x (B + C)

Page 13: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

13

Does associativity matter? In mathematics, A+B+C+D has

the same value no matter what order the addition is done in.

In programming languages the order can be important: float A, B, C, D, Sum;

Sum = A + B + C + D; // A+C or B+D

overflow is possible

Very large positive

Very large negative

Page 14: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

14

Parenthneses Programmers can alter the

precedence and associativity rules by placing parentheses in expressions.

(A+B)*C The disadvantage is that it makes

writing expressions more tedious.

Page 15: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

15

C conditional expression Ternary( 三元 ) operator : ? Page 298

Expression_1 ? Expression_2: Expression_3

x = (a < b) ? a : b; “ if (a < b) x = a;

elsex = b; ”

(a < b) ? a : b = x; “ if (a < b)a = x;

else b = x; ”

Average = (count == 0)? 0: sum/count;If (count ==0) average =0;else average = sum/count;

Page 16: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

16

Operand evaluation order Side effect—occurs when a function

changes either one of its parameters a global variable

Assume fun(x) changes parameter x y = fun(x) + x; where fun(x)= x/2; y = x + fun(x); and x = 2*x; x = 10, then y = 15 or 25

Page 17: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

17

Side effect example (1 of 2)

int a = 5;

int fun1() {a = 17;return 3;

}

int fun2 () {a = a + 5;return 7;

}

void main() {int x = fun2() + fun1();cout << a << endl;

}17 is printed

Page 18: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

18

Side effect example (2 of 2)

int a = 5;

int fun1() {a = 17;return 3;

}

int fun2 () {a = a + 5;return 7;

}

void main() {int x = fun1() + fun2();cout << a << endl;

}

22 is printed

Page 19: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

19

Overloaded Operators Using of an operator for more than one

purpose is called operator overloading Some are common (e.g., + for int and float) Some are potential trouble (e.g., *, & in C

and C++) Loss of compiler error detection

(omission of an operand should be a detectable error)

Some loss of readability Can be avoided by introduction of new

symbols (e.g., Pascal’s div)

Page 20: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

20

Overloaded operators Allowed by FORTRAN 90, Ada, C++ Overloading is accomplished by defining

functions that have the same name as the operator being overloaded

Java does not permit operator overloading

Page 21: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

21

C++ operator overload (page 302)

Ex 1.

X = z & y z AND y 運算

X = &y 傳回 y 的位址

Ex 2.

Avg = sum / count; 轉換為整數後除 再轉換為浮點數

Pascal:

div specifies integer division and / specifies floating point division

Page 22: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

22

Overloaded Operators C++ and Ada allow user-defined

overloaded operators Potential problems:

Users can define nonsense operations Readability may suffer, even when the

operators make sense

Page 23: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

23

Type conversions Type conversions are either

Narrowing (e.g., double -> float) May lose data

Widening (e.g., int -> float) Generally safe

Type conversions may be Implicit (a.k.a. coercion) Explicit

Page 24: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

24

Coercion in expressions Languages that allow mixed-mode expressions

must define conventions for coercion Pro: flexibility Con: reliability problems In Java, byte and short are coerced to int

whenever an operation is applied:int a, b, c;float d;a= b*d; //c 打成 d 無法偵測出錯誤 ( 因為變數型態自

動轉換 )

byte a, b, c;…a = b + c; // b and c are promoted to int before “+” // is applied, then result is converted to byte

// b + c 有可能超過 byte 或 short 之最大限值

Page 25: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

25

Explicit type conversions (Cast in C) Ada conversion:

integer a, b;float x;…x = float(a) / float(b);

C conversion (cast) int a, b;

float x;x = (float) a / (float) b;

Page 26: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

26

Errors in expressions Errors resulting from narrowing

conversions Errors resulting from limitations of

computer arithmetic Overflow Underflow

Some languages (Ada, C++, Java) provide an exception handling mechanism that allows the programmer to handle conditions such as divide by 0 (Page 306)

Page 27: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

27

Overflow 溢位,上溢運算結果的字的長度超過了儲存的儲存單元的長度。在暫存器中,由於運算結果超過了暫存器的長度,丟失了一位或多位最左邊的整數數字的情況。

Underflow 下溢計算器上出現的一種情況,計算結果顯示的是數的高位部分,該數的小數點右邊部分已超出了機器的輸出容量。

Errors in expressions

Page 28: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

28

Relational expressions Relational operators in various

languages: Ada: =, /=, >, <, >=, <= Java: ==, !=, >, <, >=, <= FORTRAN: .EQ. .NE. .GT. .LT. .GE. .LE.

Relational operators always have lower precedence than arithmetic operators

Page 29: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

29

Boolean Expressions Boolean Expressions

Operands are Boolean and the result is Boolean

Operators:FORTRAN 77 FORTRAN 90 C Ada

.AND. and && and

.OR. or || or

.NOT. not ! Not xor

Page 30: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

30

Boolean expressions Ada operator precedence:

**, abs, not *, /, rem +, - (unary) +, -, & (binary) =, /=, <, >, <=, >=, in, not in and, or, xor, and then, or else

In Ada, a > b and a < c or k = 0 is illegal. It can be written as either:

(a > b and a < c) or k = 0, or a > b and (a < c or k = 0)

Page 31: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

31

“ boolean” expressions in C (Page 307)

C has no boolean type; instead, numeric values represent true and false 0 = false any other value = true

In C, the expression a > b > c is legal, but not meaningful

Readability dictates that a language should include a boolean type instead of using integer values to represent the result of logical expressions

Page 32: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

32

Short-circuit evaluation A short-circuit evaluation of an expression is one in

which the result is determined without evaluating all of the operands and/or operators.

The value of (a >= 0) and (b < 10) is independent of the second relational expression if a < 0.

Lack of short circuit evaluation can cause problems. Index = 1;

while ((Index < listlen) && (list[Index] != key)) Index = Index + 1;

If the expression doesn’t short-circuit, then both operands to “&&” are evaluated.

Page 33: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

33

Short-circuit evaluation Short-circuit evaluation exposes the

problem of side effects in expressions:if (a > b) || (b++/3)

If a > b, the second expression which increments b is not evaluated.

Ada has short-circuit logical operators and then and or else : (page 309)if (index <= listlen) and then (list(index) /=

key)

Page 34: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

34

Assignment Statements The operator symbol :

1. “=“ for FORTRAN, BASIC, PL/I, C, C++, Java

2. “:=“ for ALGOLs, Pascal, Ada “=“ Can be bad if it is overloaded for the

relational operator for equalitye.g. (PL/I)

A = B = C; Results different from C

Page 35: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

35

Assignment statements Simple assignment:

a = b a = b = c

Suppose a, b, and c are integers In PL/I, the result of b = c is boolean, so the

boolean value true or false is assigned to a In C, the integer value of c is assigned to b, which

is in turn assigned to a (multiple targets) The assignment operator in C returns a

value, so the expression a = b can be used wherever an expression of its type is appropriate

Page 36: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

36

Assignment statements Multiple targets

Sum, Total = 0 (PL/I) Sum = Total = 0 (C)

Conditional targets flag ? count1 : count2 = 0; (C, C++, and

Java)

Compound assignment operators: a += b; /* a = a + b; */ (C, C++, and Java)

Page 37: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

37

Assignment statements Unary assignment operators

sum = count++; sum = count; count = count

+1; sum = ++count; count = count +1;

sum = count; count ++; count = count +1; - count ++; - (count ++);

not (- count) ++

Page 38: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

38

Assignment statements Assignment as an expression

In C, C++, and Java the assignment statement produces a result.

Thus, the assignment statement can be used like an expression:

while ((ch = getchar()) != EOF) { … } This capability is often the source of subtle

errors( 些微的錯誤 )

Page 39: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

39

Assignment statements If we type if (x=y)

instead of if (x==y)which is an easy mistake to makenot detectable as an error by compiler

Three design decisions: Allowing assignment to behave like an

ordinary binary operator Using arithmetric expressions as Boolean

operands Using two very similar operators, = and ==

Page 40: 1 Chapter 7 Expressions and assignment statements Robert W. Sebesta.

40

Mixed-Mode Assignment In FORTRAN, C, and C++, any numeric value

can be assigned to any numeric scalar variable; whatever conversion is necessary is done

In Pascal, integers can be assigned to reals, but reals cannot be assigned to integers (the programmer must specify whether the conversion from real to integer is truncated or rounded)

In Java, only widening assignment coercions are done

In Ada, there is no assignment coercion