Operators Flashcards
What is operator precedence?
The order in which operators are evaluated in a compound expression.
When two operators of the same precedence level are next to each other, what determines the order in which they are evaluated (L to R or R to L)?
Associativity rules.
What is a prefix vs. postfix increment/decrement operator
prefix: ++x, –x
postfix: x++, x–
Describe the difference between how the prefix and postfix increment operators work?
The prefix increment/decrement operators are very straightforward. The value of x is incremented or decremented, and then x is evaluated.
The postfix increment/decrement operators are a little more tricky. The compiler makes a temporary copy of x, increments x, and then evaluates the temporary copy of x.
What is a side effect?
a result of an operator, expression, statement, or function that persists even after the operator, expression, statement, or function has finished being evaluated; a permanent change of a value.
T/F: it doesn’t really matter if you use an operator with side effects inside of compound expressions.
F; it’s a good idea to avoid this b/c C++ b/c parameters to functions (for example) are evaluated in a random order, which means you can get different results depending on how the variables are used:
int x = 5;
int nValue = Add(x, ++x);
May evaluate to either 11 or 12.
T/F: any operator which causes side effects should be placed in its own statement?
T; this can wreak havoc otherwise.
How does the comma operator evaluate multiple expressions; what does ‘z’ evaluate to in the following snippet:
int x = 0;
int y = 2;
int z = (++x, ++y);
It uses the rightmost operand; z will be assigned the value of 3 b/c ‘++y’ is being evaluated.
T/F: The comma operator works great with for loops.
T; it allows you to define and manipulate multiple variables with each iteration:
for (int iii = 1, jjj = 10; iii <= 10; iii++, jjj–){}
What is the syntax for the conditional (ternary) operator?
x = (condition) ? some value : some other value;
What is the safest way to test for equality with floating point values?
Use epsilon:
bool IsEqual(double dX, double dY) { const double dEpsilon = 0.000001; // or some other small number return fabs(dX - dY) module that returns the absolute value of a number)
What’s wrong with the following snippet:
int x = 5;
int y = 7;
if (! x == y)
cout «_space;“x does not equal y”;
else
cout «_space;“x equals y”;
the if statement is saying (!x) == y, which is false (!x will evaluate to 0), so the statement will say that they are equal. The correct way to write the if statement if:
if(!(x == y))
T/F: if is best practice to put operands and other operators in parentheses when using ‘!’
T; ‘!’ i intended to operate on the result of other operators. The exception to this rule is if you are negating a single value: (!bValue), as ‘!’ has only one operand to work on so no conflicts will emerge.
What is short-circuit evaluation?
It is used by logical AND (‘&&’) to automatically evaluate an entire statement to false without evaluating the other operands if the first statement if false; this always means the statement will evaluate to false regardless of the other operands.
What is the best practice when mixing && and ||?
Enclose everything in parentheses; && has a higher precedence than ||, so leaving out parentheses can cause incorrect evaluations.