问题描述
bool finished = is_done();
if(finished&& try_again()){}
我的理解短路评估是正确的,如果完成是
false,try_again()永远不会被执行?
或者它有时会得到评估吗?
是的。
-
Jean-Marc
不,它不会评估完成后是否为0.
因此,这是正确的:
返回0&& 1/0;
表达式(0&& 1/0)的评估如下:
(a&& ; b)
首先,评估a。如果a为假,那么整个表达式为$ / b $ b替换为0.(false)
如果a为真,则计算b,并将表达式替换为
是真还是假,取决于评估的是什么。
这也是正确的
返回1 || 1/0;
不,它不会评估完成后是否为0.
因此,这是正确的:
返回0&& 1/0;
表达式(0&& 1/0)的评估如下:
(a&& ; b)
首先,评估a。如果a为假,那么整个表达式为$ / b $ b替换为0.(false)
如果a为真,则计算b,并将表达式替换为
是真还是假,取决于评估的是什么。
这也是正确的
返回1 || 1/0;
哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇假QUOT;从左到右匹配
&&声明退出并返回false。
bool finished = is_done();
if (finished && try_again()) { }
Is my understanding of short circuit evaluation correct that if finished is
false that try_again() will never be executed?
Or does it get evaluated sometimes?
Yes.
--
Jean-Marc
No, it does not evaluate if finished is 0.
Thus, this is correct:
return 0 && 1 / 0;
The expression (0 && 1 / 0) is evaluated like this:
(a && b)
First, a is evaluated. If a is false, then the whole expression is
replaced by 0. (false)
If a is true, then b is evaluated, and the expression is replaced by
true or false, depending on what b was evaluated.
This is also correct
return 1 || 1 / 0;
No, it does not evaluate if finished is 0.
Thus, this is correct:
return 0 && 1 / 0;
The expression (0 && 1 / 0) is evaluated like this:
(a && b)
First, a is evaluated. If a is false, then the whole expression is
replaced by 0. (false)
If a is true, then b is evaluated, and the expression is replaced by
true or false, depending on what b was evaluated.
This is also correct
return 1 || 1 / 0;
Wow what a long winded way of explaining something.
Short answer : as soon as one "false" is matched from left to right the
&& statement "exits" and returns false.
这篇关于短路评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!