Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the order of operations change within an if expression?

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 
like image 913
dolphy Avatar asked Jul 21 '11 21:07

dolphy


People also ask

What happens if two operators on the same precedence level appear in an expression?

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.

Which of the following can be used to change the order of operation in an expression?

Answer. Brackets — () can be used to change operator precedence.

What is the correct order of precedence in evaluating an expression or equation in programming?

It stands for Parentheses, Exponents, Multiplication/Division, Addition/Subtraction. PEMDAS is often expanded to the mnemonic "Please Excuse My Dear Aunt Sally" in schools.

What has the highest order of operation in an expression?

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.


1 Answers

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) ++.

like image 102
Steve Jessop Avatar answered Oct 04 '22 18:10

Steve Jessop