Chapter 7

Télécharger la présentation

Chapter 7

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. Chapter 7 Expressions and Assignment Statements

2. Chapter 7 Topics • Introduction • Arithmetic Expressions • Overloaded Operators • Type Conversions

3. Introduction • Expressions are the fundamental means of specifying computations in a programming Language . • An expression is a piece of a statement that describes a series of computations to be performed on some of the program’s variables . • The value of almost all expressions depends on the order of evaluation of the operators in the expression . • Arithmetic expressions consist of operators , operands, parentheses, and functioncalls . 15 * (a + b) / log(x)

4. Some Terminology • An Operation is something machine does, such as the operation “ addition ". • An Operator is the symbol we use in a program to write the operation, such as the operator +. • An Operand is a thing that an operation is applied to. For example, in the expression and “ 7 aa + 7 “ there are two operands .

5. Arithmetic Expressions • Arithmetic expressions can contain arithmetic operands, arithmetic operators, and parentheses. • There must always be at least one operand. • The operands can belong to any of the four arithmetic data types (integer, real, double precision, or complex); the result also has an arithmetic data type .

6. Some Terminology • A Unary operator has one operand . y , &p, !f • A Binary operator has two operands . x*y, x/y,x&&y, x&y, x-y • A Ternary operator has three operands . x? y :z

7. Arithmetic Expressions: Design issues for arithmetic expressions : • Operator Evaluation Order . • Operand Evaluation Order . • Operand evaluation side effects . • Operator overloading . • Mode mixing expressions .

8. Operator Evaluation Order We can illustrate the rule of operator evaluation order as follows: 1- Operator precedence : Operator precedence is why the expression 5 + 3 * 2 is calculated as 5 + (3 * 2) , giving 11, and not as (5 + 3) * 2, giving 16 We say that the multiplication operator (*) has higher “precedence" or “priority" than the addition operator (+), so the multiplication must be performed first .

9. Languages and Precedence • The formation rules for expressions imply that the defined unary operators have highest precedence of all operators, and defined binary operators have the lowest precedence of all operators. • APL only uses one level of precedence in its language.

10. 2- Operator Associativity: • Associativity is a property that a binary operation can have. It means that the order of evaluation is immaterial if the operation appears more than once in an expression. Put another way, no parentheses are required for an associative operation. Consider for instance the equation (1+2)+5=1+(2+5) • Adding 5 and 2 gives 7, and adding 1 gives an end result of 8 for the left hand side. To evaluate the right hand side, we start with adding 2 and 1 giving 3, and then add 5 and 3 to get 8, again. So the equation holds true. In fact, it holds true for all real numbers, not just for 5, 2 and 1. We say that "addition of real numbers is an associative operation".

11. Languages and Associativity • Associativity in common imperative languages is left to right, except that the exponentiation operator associates right to left. • In Ada, exponentiation is nonassociative, thus we must find solution to this problem we will discuss it in parentheses. • In Visual Basic, the exponentiation operator,^, is left associative.

12. 3- Parentheses: • Precedence and associativity rules can be overridden with parentheses . The following is an example of an arithmetic mean (average) of five terms: M= (a + b + c + d + e ) / 5 • The parentheses are required, because division has higher precedence than addition. The entire quantity ( a + b + c + d + e ) is to be divided by 5. If the parentheses are erroneously omitted, we obtain a + b + c + d + e / 5 , which evaluate as : a + b + c + d +

13. Languages and Parentheses • In all of the imperative languages, the unary minus operator can appear in an expression either at the beginning or anywhere inside the expression, as long as it is parenthesized to prevent it from being adjacent to another operator. For example: X + (- Y ) * Z • In Ada, because the exponentiation is nonassociative, which means that the expression X ** Y ** Z is illegal in Ada. Such an expression must be parenthesized to show the desired order, as in either )X ** Y) ** Z or X ** ( Y ** Z)

14. 4-Conditional Expression: • The conditional operator (? : ) sometimes can be used in place of an if...else statement . This is a ternary operator, it takes three operands. System.out.println( StudentGrade>=75 ? "Passed" : "Failed");

15. Operand Evaluation Order • Variables: fetch the value from memory . • Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction . • Parenthesized expressions: evaluate all operands and operators first .

16. Side Effects • Side effect is a modification of variable's value, such as calling function, a functional side effect is when a function changes a two-way parameter or a nonlocal variable. It occurs when the function changes either one of its parameters or a global variable. • X + FUN(X) If FUN changes X, there is a side effect .

17. Solution for Side Effect • The first solution is that the designer could disallow function evaluation from affecting the value of expressions by disallowing functional side effects. • It is good because it always work, but some times it difficult .

18. Solution for Side Effect • The second solution to side effect problems is to state in the language definition that operands in expressions are to be written and evaluated in a specific order and demand that implementers guarantee that order. • This solution limits some compiler optimizations.

19. Solution for Side Effectin FORTRAN 77 • The designers of FORTRAN 77 state that expressions that have function calls are legal only if the functions do not change the values of other operands in the expression . • The problem with this is the compiler can have a difficult time determining the exact effect a function can have on variables outside the function .

20. Overloaded Operators • Use 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 .

21. Languages and Overloaded Operators • Pascal language use div with integer division and / with floating-point division. • C++ and Ada allow user-defined overloaded operators. • JavaScript avoids this problem by not having any integer arithmetic. • In PHP, if two integers are divided and the result is not an integer, a floating-point value is produced.

22. Type Conversions • Type conversion or typecasting refers to changing an entity of one data type into another. • Narrowing conversion ( float to int ). • Widening conversion ( int to float ). • There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion.

23. 1-Coercion in Expression • A mixed-mode expression is one that has operands of different types. • Disadvantage of coercions: They decrease in the type error detection ability of the compiler. • In most languages, all numeric types are coerced in expressions, using widening conversions. • In Ada, there are virtually no coercions in expressions.

24. 2- Explicit Type Conversions • Called casting in C-based language . • Examples : C: (int) angle Ada: Float (sum) Note that Ada’s syntax is similar to function calls .

25. Errors in Expressions Causes : • Inherent limitations of arithmetic e.g., division by zero • Limitations of computer arithmetic e.g. overflow • Often ignored by the run-time system.