和 && 运算符评估哪个顺序
例如下面的代码
if (float alpha = value1-value2 && alpha > 0.001)
//do something
抛出了一个异常,即 alpha 正在被使用而没有被启动。我认为 && 左边的表达式总是会首先启动 alpha 的值,但似乎我可能错了
任何的想法?
谢谢
和 && 运算符评估哪个顺序
例如下面的代码
if (float alpha = value1-value2 && alpha > 0.001)
//do something
抛出了一个异常,即 alpha 正在被使用而没有被启动。我认为 && 左边的表达式总是会首先启动 alpha 的值,但似乎我可能错了
任何的想法?
谢谢
这被解析为:
if (int alpha = (value1-value2 && (alpha > 0.001)))
...因为&&
“解析优先级”比=
-- 这可能不是你想要的。尝试:
int alpha = value1-value2;
if (alpha && (alpha > 0.001))
The bottom line here is that what you are trying to express cannot be possibly expressed by a single logical condition with the declaration of alpha
being embedded into it (despite what some other answers claim).
The other answers already explained to you that your condition is not parsed the way you think it is parsed, although many answers make an obvious error of referring to the precedence of =
operator in the condition, while in reality there's no =
operator there whatsoever. The correct explanation is that when you declare a variable in the if
condition, the syntax is that of declaration with an initializer, so the whole thing is parsed the same way as
int alpha = value1 - value2 && alpha > 0.001;
would be parsed, i.e. it is a declaration of int alpha
initialized with value1 - value2 && alpha > 0.001
. There's no operator =
in it. And I hope now you can see why the compiler says that you are reading an uninitialized variable in the initializer expression. The compiler would make the same complaint on the following declaration
int alpha = alpha; // reading uninitialized variable
for the very same reason.
To achieve what you are literally trying to express, you have to either pre-declare alpha
int alpha;
if ((alpha = value1 - value2) && alpha > 0.001) {
// whatever
}
or split your if
into two
if (int alpha = value1 - value2)
if (alpha > 0.001) {
// whatever
}
However, since the second condition already requires alpha
to be greater than 0
, it doesn't make much sense to even verify the first one, so the most meaningful thing to do would be to just reduce the whole thing to
int alpha = value1 - value2;
if (alpha > 0.001) {
// whatever
}
Of course, as others already noted, the comparison of an int
value to 0.001
is a valid, but rather weird thing to do. Just do
int alpha = value1 - value2;
if (alpha > 0) {
// whatever
}
左侧总是首先被评估。这里的问题是运算符优先级。看看这是否效果更好:
if ((int alpha = value1-value2) && (alpha > 0.001))
在阐明它是如何工作的(出于教育目的)之后,不要这样做。不要在同一语句中混合变量初始化/赋值和比较。
如果每个语句单独是一个“命令”或一个“查询”会更好。
如果“if”中的条件非常清晰易读并且明确地布尔值,那就更好了。不是整数,没有什么,只是布尔。
您的条件的第一部分是一个整数。然后你用一个布尔值做一个和。您完全不需要强制转换。准确地给出 if 和条件运算符的要求:布尔值。
回答标题中的问题,取决于操作数的类型。
对于内置类型,&&
短路,这意味着评估 LHS,如果它为假,则根本不评估 RHS。
对于重载的用户定义类型operator&&
,它不会短路。双方都以未指定的顺序进行评估,然后调用该函数。
不过,我认为其他人已经处理了您需要回答的问题。
如果我没记错的话,该操作是未定义的。分配给变量然后在单个语句中引用相同的变量是未定义的。
我通常总是使用括号只是为了让我的代码更清楚我的意图。
它是从左到右评估的。
但是,在您的情况下,分配是最后发生的事情,它们的整个行为将表现为(其中 alpha 用作计算的一部分以获取结果以对其进行初始化):
if (int alpha = (value1-value2 && alpha > 0.001))
您不能将变量声明混合到复杂的表达式中,因此以下内容将无法编译:
if ((int alpha = value1-value2) && (alpha > 0.001))
因此,您需要将其分成两行:
int alpha = value1 - value2;
if (alpha > 0.001)
我的猜测是,这是因为您在“if”语句中声明了存储。我什至不认为那会编译。
试试这个。
int alpha;
if ((alpha=value1-value2) && alpha>0.001)
但我不认为这是你需要的。您将 alpha 作为 int,然后将其与浮点值进行比较。只要 alpha 不为零,&& 语句的第一部分将返回 true,如果 alpha 大于 0,则第二部分将返回 true。所以你应该这样做
int alpha;
if ((alpha=value1-value2)>0)
或者更多可读的代码
int alpha=value1-value2
if (alpha>0)
但是要回答您最初的问题: && 在答案显而易见时从左到右执行并短路。即,如果 && 的第一部分为假,则第二部分甚至不被评估!
这是一篇列出运算符优先级和关联性的文章。
据我所知,您的意图是声明一个临时值,为其分配 value1-value2 的值,然后测试结果并输入 if 块,如果它大于某个值。 alpha
被声明为 int,但您似乎将其与 double 进行比较。alpha 应该是双精度的。
你在使用临时工时很有创意。清晰往往胜于可爱。改为这样做:
double alpha = value1-value2;
if (alpha > 0.001)
根据: http ://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
- LtR
> LtR
&& LtR
= RtL
给出你的例子
(int alpha = value1-value2 && alpha > 0.001)
(int alpha = (value1-value2) && alpha > 0.001)
(int alpha = (value1-value2) && (alpha > 0.001))
(int alpha = (value1-value2) && (alpha > 0.001))
(int alpha = ((value1-value2) && (alpha > 0.001)))
As written this expression does the following:
value1 - value2
and converts it to a bool
by implicit comparison against zero - i.e., it is effectively (value1 - value2) != 0
alpha > 0.001
after truncating 0.001
to int(0)
. At this point alpha
is not initialized.I think that this summarizes the rest of the posts. The only reason that I posted a separate answer is that I could not find one that mentioned both when alpha
was not initialized and all of the conversions that are occurring here; wallyk's answer is closest.
Of course, the rest of the answers that suggest that you use parentheses and a separate declaration of alpha
are exactly what you should do to fix this. Declaring variables within an if
statement is part of the language that I haven't found a good use for - declarations within repetition structures seems more appropriate.
问题是该语句的评估是这样的:
if (int alpha = (value1-value2 && alpha > 0.001))
使用括号固定 && 运算符的左侧和右侧:
if ((int alpha = value1-value2) && (alpha > 0.001))