if表达式中的操作顺序会改变吗? [英] Does the order of operations change within an if expression?

查看:110
本文介绍了if表达式中的操作顺序会改变吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了一些我以为可以立即理解的东西,但是对此进行更多的思考,我想了解它为什么以这种方式起作用.

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.

请考虑以下代码.显然(x-- == 9)得到评估,而(y++ == 11)没有得到评估.我的第一个想法是逻辑&&插入,发现该表达式已经为假,并在计算该表达式的第二部分之前将其踢出.

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.

我思考得越多,我就越不明白为什么会这样.据我了解,逻辑运算符按优先级在增量操作之下.即使整个表达式已经变为假,也应该不对(y++ == 11)求值吗?

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?

换句话说,在if语句认识到整个表达式为假之前,运算顺序是否应该指示必须对(y++ == 11)进行求值?

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;
}

输出:

Final X: 9
Final Y: 10

推荐答案

逻辑运算符按以下顺序降到增量运算以下 优先.

logical operators fall below increment operations in the order of precedence.

优先顺序是 not 执行顺序.它们是完全不同的概念.优先顺序仅在执行操作数先于其运算符之前对其进行评估的情况下才会影响执行顺序,而优先顺序可帮助您了解每个运算符的操作数是什么.

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.

短路运算符是部分例外,即使操作数先于运算符也要评估,因为它们评估的是LHS,则运营商有权决定是否评估RHS,可能是 评估RHS,然后计算运算符的结果.

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.

不要认为更高优先级的操作先执行".认为它们绑定更紧密". ++的优先级高于&&,在表达式x ++ && y ++中,运算符优先级意味着++y的绑定更紧密,与&&绑定,因此表达式整体等效于(x++) && (y++),而不是(x++ && y) ++.

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表达式中的操作顺序会改变吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆