Monday, April 8, 2013

Chapter 7 - Expressions and Assignment Statements

Review Question
1.The operator precedence rules for expression evaluation partially define the order in which the operators of different precedence levels are evaluated. The operator precedence rules for expressions are based on the hierarchy of operator priorities, as seen by the language designer. When an expression contains two adjacent 2 occurrences of operators with the same level of precedence, the question of which operator is evaluated first is answered by the associativity rules of the language. An operator can have either left or right associativity, meaning that when there are two adjacent operators with the same precedence, the left operator is evaluated first or the right operator is evaluated first, respectively.
2.It is a conditional operator that provides a shorter syntax for the if..then..else statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third operand is returned.
3.The operators ++ for increment, and –– for decrement, can be used either in expressions or to form stand-alone single-operator assignment statements. They can appear either as prefix operators, meaning that they precede the operands, or as postfix operators, meaning that they follow the operands.
5.Operator with illegal expression.
8.A side effect of a function, naturally called a functional side effect, occurs when the function changes either one of its parameters or a global variable. (A global variable is declared outside the function but is accessible in the function.)
9. coercion is an automatic conversion. For example, if an int variable and a float variable are added in Java, the value of the int variable is coerced to float and a floating-point add is done.
11. In object-oriented programming, operator overloading—less commonly known as operator ad-hoc polymorphism—is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by the language, the programmer, or both.
12. A narrowing conversion converts a value to a type that cannot store even approximations of all of the values of the original type. A widening conversion converts a value to a type that can include at least approximations of all of the values of the original type.
28.Cast is explicit type conversions. To specify a cast, the desired type is placed in parentheses just before the expression to be converted.
Problem Set
1. Suppose Type1 is a subrange of Integer. It may be useful for the difference between Type1 and Integer to be ignored by the compiler in an expression.
2.If operands in an expression contains both INTEGER and REAL constants or variables, this is a mixed mode arithmetic expression. In mixed mode arithmetic expressions, INTEGER operands are always converted to REAL before carrying out any computations. As a result, the result of a mixed mode expression is of REAL type. can coercion be used in some cases of type mismatch? Fortran, C, C++, and Perl use coercion rules for mixed-mode assignment that are similar to those they use for mixed-mode expressions; that is, many of the possible type mixes are legal, with coercion freely applied.7 Ada does not allow mixed-mode assignment. In a clear departure from C++, Java and C# allow mixed-mode assignment only if the required coercion is widening.8 So, an int value can be assigned to a float variable, but not vice versa. Disallowing half of the possible mixedmode assignments is a simple but effective way to increase the reliability of Java and C#, relative to C and C++.
3. The elimination of overloaded operators in my favorite language would not be beneficial. Operator overloading is claimed to be useful because it allows the developer to program using notation "closer to the target domain"[1] and allows user-defined types a similar level of syntactic support as types built into the language. It can easily be emulated using function calls; for an example, consider the integers a, b, c:
7. An expression such as a + fun(b)
8. Consider the integer expression A + B + C. Suppose the values of A, B, and C are 20,000, 25,000, and -20,000, respectively. Further suppose that the machine has a maximum integer value of 32,767. If the first addition is computed first, it will result in overflow. If the second addition is done first, thewhole expression can be correctly computed.
10. (a) ( a * ( b – ( 1 + c )1 )2 )3
(b) ( a * ( ( b – 1 )2 / ( c mod d )1 )3 )4
(c) ( ( a – b )5 / ( c & ( d * ( e / ( a – 3 )1 )2 )3 )4 )6
(d) ( – ( a or ( c = ( d and e )1 )2 )3 )4
(e) ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4 (f) ( – ( a + b )1 )2

No comments:

Post a Comment