Chapter 7 Problems



Chapter 7 Review Questions

1. Define operator precedence and operator associativity.
-  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. What is a ternary operator?
- 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. What is a prefix operator?
- 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. What is a nonassociative operator?
- Non-associative operators are operators that have no defined behavior when used in sequence in an expression. In Prolog, the infix operator :- is non-associative because constructs such as "a :- b :- c" constitute syntax errors. Another possibility distinct from left- or right-associativity is that the expression is legal but has different semantics. An example is the comparison operators (such as >, ==, and <=) in Python: a < b < c is shorthand for (a < b) and (b < c), not equivalent to either (a < b) < c or a < (b < c).

8. Define functional side effect.
- 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.)

11. What is an overloaded operator?
- 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. Define narrowing and widening conversions.
- 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.

Problem Set

1. When might you want the compiler to ignore type differences in an expression?
When you want to evaluate a string as a number and vice-versa. For example, generally speaking, most compilers treat user input as a string. Suppose you are creating a calculator program. You want the compiler to treat each of the numbers the user inputs as numbers, not as strings, so you can run math over them without having to explicitly cast the user input to floats or singles.

2. State your own arguments for and against allowing mixed-mode arithmetic expressions.
- 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. Do you think the elimination of overloaded operators in your favorite language would be beneficial? Why or why not?
- 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. Describe a situation in which the add operator in a programming language would not be commutative.
- vector(1) v0
scalar alpha = 1
!a = 1

vector v1 = v0+alpha
vector v2 = alpha+v0

vector v3 =  v0+!a
vector v4 = !a+v0

8. Describe a situation in which the add operator in a programming language would not be associative.
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, the
whole expression can be correctly computed.