# 程式語言 Chapter 7 - Expressions and Assignment Statements ## Evaluation of Expressions * Variables * Values fetched from memory * Constants * Sometimes fetched from memory * Part of the machine language instruction (memory fetch is not required) * Parenthesized expression * Operators in the parenthesised expression must be evaluated first before the expression can be used as an operand. ## Side Effects and Evaluation Order * Functional side effect: function which changes * its parameters * or global variables ### Example ``` a + fun(a) ``` * No side effect * The order of evaluation has no effect on the value of the expression. * With side effect * If fun() changes a, a side effect happend. * Different evaluation order causes different value of the expression. ## Operator Overloading (Multiple Use of Notations) * Acceptable as long as **readability** and/or **reliability** do not suffer. * The "+" arithmetic operator * Integer addition * Floating-point addition * String catenation * The ampersand(&) in C * Bitwise logical AND (Binary 二元) * Address-of operator (Unary 一元) * Same symbol(&) for two completely unrelated operations -> detrimental to readability * Leave out on of the operands -> an error undected by the compiler ## Short-Circuit Evaluation * Determine result without evaluating all of the operands and/or operators. * Arithmetic expression * Not easy to detect during execution -> Never taken ``` 0 * fun(b) ``` * Boolean expression * Something like the truth table * Easier to discover -> Taken * Potential Problem with Non-Short-Circuit Evaluation * Indexing error * Use short-circuit-evaluation to solve ``` while ((index < listlen) && (list[index] != key)) // IndexOutOfBoundException ``` * Errors from Short-Circuit-Evaluation * Part of the expression that contains a side effect is not evaluated. ``` (a > b) || (b++ / 3) ```