短路评估 [英] short circuit evaluation

查看:149
本文介绍了短路评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bool finished = is_done();


if(finished&& try_again()){}


我的理解短路评估是正确的,如果完成是

false,try_again()永远不会被执行?

或者它有时会得到评估吗?

解决方案

" Lassie" < zh*@woaini.comwrites:


if(finished&& try_again()){}


我对短路评估的理解是否正确,如果完成

为假,则try_again()永远不会被执行?



是的。


-

Jean-Marc


9月16日下午6:55,Lassie < z ... @ woaini.comwrote:


bool finished = is_done();


if(finished&) ;& try_again()){}


我对短路评估的理解是正确的,如果完成了

false,则try_again()永远不会被执行?

或者它有时会得到评估吗?



不,它不会评估完成后是否为0.


因此,这是正确的:


返回0&& 1/0;


表达式(0&& 1/0)的评估如下:


(a&& ; b)


首先,评估a。如果a为假,那么整个表达式为$ / b $ b替换为0.(false)

如果a为真,则计算b,并将表达式替换为

是真还是假,取决于评估的是什么。


这也是正确的


返回1 || 1/0;


vi ****** @ gmail.com 写道:


9月16日下午6:55,Lassie < z ... @ woaini.comwrote:


> bool finished = is_done();

if(finished&& ; try_again()){}

我对短路评估的理解是否正确,如果完成是错误的,那么try_again()将永远不会被执行?
或者它是否得到评估有时?




不,它不会评估完成后是否为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?

解决方案

"Lassie" <zh*@woaini.comwrites:

if (finished && try_again()) { }

Is my understanding of short circuit evaluation correct that if finished
is false that try_again() will never be executed?

Yes.

--
Jean-Marc


On Sep 16, 6:55 pm, "Lassie" <z...@woaini.comwrote:

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?


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;


vi******@gmail.com writes:

On Sep 16, 6:55 pm, "Lassie" <z...@woaini.comwrote:

>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?



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.


这篇关于短路评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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