450 exam 3 Flashcards
(182 cards)
unary operator
has one operand; e.g., ++, –
binary operator
has two operands; e.g., +, -, *, /
ternary operator
has three operands; e.g., ? : conditional expression syntax (ex: average = (count == 0) ? 0 : sum / count;)
operator precedence rules for expression evaluation define
the order in which “adjacent” operators of different precedence levels are evaluated
typical precedence levels (highest to lowest precedence)
parentheses unary operators ** (if the language supports it) *, / \+, -
operator associativity rules for expression evaluation define
the order in which adjacent operators with the same precedence level are evaluated
associativity for operators is typically ? except for ?
left to right; ** is right to left
what does 2 ** 3 ** 4 evaluate to using typical associativity rules?
2 ** 81
associativity for assignment is ?
right to left
what are the values of a=1, b=2, c=3 after executing a = b += c ?
a=5, b=5, c=3
functional side effects
occur when a function changes either one of its parameters or a global variable (declared outside the function but is accessible in the function)
functions in mathematics (do/do not) have side effects - why?
do not b/c there is no notion of variables in mathematics
functional programming languages have no variables, so do they have functional side effects?
no
a program has referential transparency if
any two expressions in the program that have the same value can be substituted for one another anywhere in the program, without affecting the action of the program
advantage of referential transparency
semantics easier to understand
programs in pure functional languages (are/are not) referentially transparent - why?
are; b/c they do not have variables (so, if a function uses an outside value, it must be a constant)
narrowing conversion
converts a value to a type that cannot include all of the value of the original type (ex: float to int)
widening conversion
a value is converted to a type that can include all of the value of the original type (ex: int to float)
coercion
implicit type conversion
casting
explicit type conversion
relational expressions
use relational operators/operands of various types; evaluate to true or false; operators !=, >=, <=, etc.
boolean expressions
operands are boolean and the result is boolean; operators &&, ||, !
short-circuit evaluation
the result is determined without evaluating all of the operands and/or operators
when would the following be an example of short-circuit evaluation?
(13 * a) * (b / 13 - 1)
if a=0, there is no need to evaluate (b / 13 -1) because the result will be 0