Chapter 7 - Expressions and Assignment Statements Flashcards
What are the 6 design issues with arithmetic expressions?
1) what are operator precedence rules
2) what the operator associativity rules
3) what is the order of operand evaluation
4) are there restrictions on operand evaluation side effects?
5) is user-defined operator overloading allowed?
6) what mode mixing is allowed in expressions
What are the definitions of unary, binary and ternary operators
unary = has one operand
binary = has two operands
ternary = has three operands
What is the typical order of operator precedence
parentheses unary operators ** (exponentiation) *,/ \+,-
just like you learned in middle school
Examples of unary operators
’-‘ (ex: -5 changes five to its negative value)
’+’ (ex: +5 this is valid syntax but it does ABSOLUTELY NOTHING)
’++’ and ‘–’
What are the typical associativity rules?
exactly what you learned in school for the most part
left to right
EXCEPT for exponentiation operator which is right-to-left so:
2 ** 3 ** 4 == 2 ** (3 ** 4)
What is the order of operand evaluation?
1) variable - fetch the values
2) constants - fetch value
3) parenthesized expressions (evaluate fully)
4) function references
definition of functional side effects
when a function changes a two-way parameter or non-local variable
2 solutions for handling expressions containing function references that produce functional side effects relevant to other values in the expression
1) don’t allow function side effects by disallowing two-way parameters and non-local references
2) make the language force operand evaluation order to be fixed
2 disadvantages of operator overloading
detriment to readability
loss of compiler error detection
definition of narrowing conversion
one that converts an object to a type that may not be able to hold some of the possible values of the original type
definition widening conversion
one that converts an object to a type that can at least approximate all the possible values of the original type if not hold them exactly
definition of a mixed-mode expression
an expression that contains operands of different types
Explicit type conversion vs implicit type conversion
explicit = conversion performed by some directive provided by the programmer (ie. casting)
implicit = conversion performed by the compiler with no directive from the programmer (ie, narrowing and widening conversions, coercion)
definition of a relational operator
an operator that compares the values of its two operands. The result of a relational expression is a boolean
ex) ==, !=
Precedence arithmetic ops, relational ops, and boolean ops
highest -postfix arithmetic ops (++ and --) -unary ops normal arithmetic ops relational ops: less than/greater than relational ops: equal/not equal boolean ops: AND boolean ops OR