Expressions and assignment statements

Download Report

Transcript Expressions and assignment statements

Chapter 7 Expressions and assignment statements

Robert W. Sebesta 1

Major points

      Arithmetic expressions Overloaded operators Type conversions Relational and boolean expressions Short-circuit evaluation Assignment statements 2

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 3

 

Arithmetic expressions

Consist of operators( 運算子 ), operands( 運算 元 ), parentheses, and function calls Operators can be  Unary - A unary operator has one operand   Binary - A binary usually infix operator has two operands, Ternary - A ternary operator has three operands

infix notation

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

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 5

Operator precedence

 Example : a + b * c a=3, b=4, c=5; If evaluated left to right , (3+4)*5= 35 If evaluated right to left , 3+(4*5)= 23 6

Operator precedence

   The operator precedence rules for expression evaluation define the order in which “ adjacent ” operators of different precedence levels are evaluated “ adjacent ” operand means they are separated by at most one Typical precedence levels 1. Parentheses ( 括號 ) ( 優先次序 ) 2. unary operators ( 單元運算子 ) 3. ** ( 指數, if the language supports it) 4. *, / ( 乘除 ) 5. +, - ( 加減 ) 7

Operator precedence

 Operator precedence in some languages: FORTRAN ** *, / all +, Pascal *, /, div, mod all +, C-based PL postfix( 後置 ) ++,- prefix( 前置 ) ++,- unary +, *, /, % binary +, Ada **, abs *, /, mod, rem unary +, 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 8

  

Operator associativity

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 9

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 + 10

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 11

APL

   All operators have the same precedence All operators are right to left associative Examples:   A x B x C  A x B + C  A x (B x C) A x (B + C) 12

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 Very large positive overflow is possible Very large negative 13

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.

14

C conditional expression

Ternary(

三元

) operator : ? Page 298 Expression_1 ? Expression_2: Expression_3 x = (a < b) ? a : b; “ if (a < b) else x = a; x = 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;

15

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); x = 10, then y = 15 or 25 and x = 2*x; 16

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 17

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 18

  

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

) 19

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 20

C++ operator overload

(page 302)

Ex 1.

X = z & y X = &y Ex 2.

z AND y

傳回 運算

y

的位址

Avg = sum / count;

轉換為整數後除 再轉換為浮點數

Pascal: div specifies integer division and / floating point division specifies

21

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 22

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 23

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 之最大限值 24

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; 25

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 divide by 0 (Page 306) that allows the programmer to handle conditions such as 26

Errors in expressions

Overflow

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

Underflow

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

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 28

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

29

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 It can be written as either:   (a > b and a < c) or k = 0 , or a > b and (a < c or k = 0) is illegal. 30

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 31

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.

32

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) 33

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 equality e.g. (PL/I) A = B = C; Results different from C 34

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 35

Assignment statements

   Multiple targets   Sum, Total = 0 Sum = Total = 0 (PL/I) (C) Conditional targets  flag ? count1 : count2 = 0; (C, C++, and Java) Compound assignment operators:  a += b; /* a = a + b; */ (C, C++, and Java) 36

Assignment statements

 Unary assignment operators  sum = count++;  sum = ++count;   count ++; - count ++; sum = count; count = count +1; count = count +1; sum = count; count = count +1; - (count ++); not (- count) ++ 37

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( 些微的錯誤 ) 38

Assignment statements

  If we type instead of if (x=y) if (x==y) which is an easy mistake to make not 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 == 39

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 40