I recently came across something that I thought I understood right off the bat, but thinking more on it I would like understanding on why it works the way it does.
Consider the code below. The (x-- == 9)
is clearly getting evaluated, while the (y++ == 11)
is not. My first thought was that logical &&
kicks in, sees that the expression has already become false, and kicks out before evaluating the second part of the expression.
The more I think about it, the more I don't understand why this behaves as it does. As I understand it, logical operators fall below increment operations in the order of precedence. Shouldn't (y++ == 11)
be evaluated, even though the overall expression has already become false?
In other words, shouldn't the order of operations dictate that (y++ == 11)
be evaluated before the if
statement realizes the expression as a whole will be false?
#include <iostream> using namespace std; int main( int argc, char** argv ) { int x = 10; int y = 10; if( (x-- == 9) && (y++ == 11) ) { cout << "I better not get here!" << endl; } cout << "Final X: " << x << endl; cout << "Final Y: " << y << endl; return 0; }
Output:
Final X: 9 Final Y: 10
Operator associativity. When an expression has two operators with the same precedence, the operators and operands are grouped according to their associativity. For example 72 / 2 / 3 is treated as (72 / 2) / 3 since the division operator is left-to-right associate.
Answer. Brackets — () can be used to change operator precedence.
It stands for Parentheses, Exponents, Multiplication/Division, Addition/Subtraction. PEMDAS is often expanded to the mnemonic "Please Excuse My Dear Aunt Sally" in schools.
The precedence of operations determines the order in which operations are performed within expressions. High precedence operations are performed before lower precedence operations. Since parentheses have the highest precedence, operations within parentheses are always performed first.
logical operators fall below increment operations in the order of precedence.
Order of precedence is not order of execution. They're completely different concepts. Order of precedence only affects order of execution to the extent that operands are evaluated before their operator, and order of precedence helps tell you what the operands are of each operator.
Short-circuiting operators are a partial exception even to the rule that operands are evaluated before the operator, since they evaluate the LHS, then the operator has its say whether or not to evaluate the RHS, maybe the RHS is evaluated, then the result of the operator is computed.
Do not think of higher-precedence operations "executing first". Think of them "binding tighter". ++
has higher precedence than &&
, and in the expression x ++ && y ++
, operator precedence means that the ++
"binds more tightly" to y
than &&
does, and so the expression overall is equivalent to (x++) && (y++)
, not (x++ && y) ++
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With