简体   繁体   English

运营商优先权

[英]Operator Precedence

I have a sample midterm question that I am not too sure about. 我有一个样本中期问题,我不太确定。 Here it is: 这里是:

#include <iostream.h>

void f( int i )
{
 if( i = 4 || i = 5 ) return;
 cout << "hello world\n" ;
}

int main()
{
f( 3 );
f( 4 );
f( 5 );
return 0;
}

So I understand that the logical OR operator has a higher precedence and that it is read left to right. 所以我理解逻辑OR运算符具有更高的优先级,并且从左到右读取。 I also understand that what's being used is an assignment operator instead of the relational operator. 我也明白,正在使用的是赋值运算符而不是关系运算符。 I just dont get how to make sense of it all. 我只是不明白如何理解这一切。 The first thing the compiler would check would be 4 || i 编译器检查的第一件事是4 || i 4 || i ? 4 || i How is that evaluated and what happens after that? 如何评估以及之后会发生什么?

Let's add all the implied parentheses (remembering that || has higher precedence than = and that = is right-associative): 让我们添加所有隐含的括号(记住|| 优先级高于= ,而且=右关联):

i = ((4 || i) = 5)

So, it first evaluates 4 || i 所以,它首先评估4 || i 4 || i , which evaluates to true (actually, it even ignores i , since 4 is true and || short-circuits). 4 || i ,其评估为true (实际上,它甚至忽略了i ,因为4true并且||短路)。 It then tries to assign 5 to this, which errors out . 然后它尝试为此分配5出错

As written, the code doesn't compile, since operator precedence means it's i = ((4 || i) = 5) or something, and you can't assign to a temporary value like (4 || i) . 如上所述,代码不能编译,因为运算符优先级意味着它是i = ((4 || i) = 5)或其他东西,并且你不能分配像(4 || i)这样的临时值。

If the operations are supposed to be assignment = rather than comparison == for some reason, and the assignment expressions are supposed to be the operands of || 如果由于某种原因操作应该是赋值=而不是比较== ,并且赋值表达式应该是||的操作数 , then you'd need parentheses 那你需要括号

(i = 4) || (i = 5)

As you say, the result of i=4 is 4 (or, more exactly, an lvalue referring to i , which now has the value 4). 如你说,结果i=4是4(或更确切地说,一个左值参照i ,现在具有值4)。 That's used in a boolean context, so it's converted to bool by comparing it with zero: zero would become false , and any other value becomes true . 这是在布尔上下文中使用的,因此通过将其与零进行比较将其转换为bool :零将变为false ,并且任何其他值都将变为true

Since the first operand of || ||的第一个操作数 is true, the second isn't evaluated, and the overall result is true. 是的,第二个没有评估,总体结果是真的。 So i is left with the value 4, then the function returns. 所以i留下值4,然后函数返回。 The program won't print anything, whatever values you pass to the function. 无论您传递给函数的值是什么,程序都不会打印任何内容。

It would make rather more sense using comparison operations 使用比较操作会更有意义

i == 4 || i == 5

meaning the function would only print something when the argument is neither 4 nor 5; 意思是当参数既不是4也不是5时,函数只会打印一些东西; so it would just print once in your example, for f(3) . 所以它只会在你的例子中打印一次,对于f(3)

Note that <iostream.h> hasn't been a standard header for decades. 请注意, <iostream.h>几十年来一直不是标准的标题。 You're being taught an obsolete version of the language, using some extremely dubious code. 你正在学习一种过时的语言版本,使用一些非常可疑的代码。 You should get yourself a good book and stop wasting time on this course. 你应该给自己写一本好书 ,不要在这门课上浪费时间。

The compiler shall isuue an error because expression 4 || i 编译器应该出错,因为表达式4 || i 4 || i is not a lvalue and may not be assigned. 4 || i不是左值,可能不会被分配。

As for the expression itself then the value of it is always equal to true because 4 is not equal to zero. 至于表达式本身,那么它的值总是等于true因为4不等于零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM