(cond1&& cond2&& cond3)总是从左到右评估? [英] Does (cond1 && cond2 && cond3) always evaluate left to right?

查看:91
本文介绍了(cond1&& cond2&& cond3)总是从左到右评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好


假设我想检查object1.Property1是否等于某个值,但

object1可能为null。


目前我有这样的代码:


if(object1!= null)

{

if(object1.Property == desiredValue)

{

//在这里工作

}

}


这是我的问题 - 我可以这样做吗...


if(object1!= null&& object1。物业== desiredValue)

{

//在这里工作

}


.. ..并确保条件总是从左到右评估和

一旦条件评估为假,评估就会停止?


如果我可以崩溃条件转换为if if语句,您认为

这或多或少是优雅的编码吗?


TIA

Bill

Hi There

Say I want to check if object1.Property1 is equal to a value, but
object1 could be null.

At the moment I have code like this:

if (object1 != null)
{
if (object1.Property == desiredValue)
{
//Do work here
}
}

Here is my question - Can I do this ...

if (object1 != null && object1.Property == desiredValue)
{
//Do work here
}

.... and be sure that conditions are always evaluated left to right and
the evaluation stops as soon as a condition evaluates to false ?

If I can collapse the conditions into one if statement, do you think
this is more or less elegant coding?

TIA
Bill

推荐答案

是的,你可以。

这些陈述是evalueat从左到右。最正确的条件

在以前的条件之一评估为假时不会被评估。


或******* @ yahoo。 com.au"写道:
Yes, you can.
The statements are evalueated from left to right. The most right condition
is not evaluated when one of the former conditions evaluates to false.

"or*******@yahoo.com.au" wrote:
你好

说我想检查object1.Property1是否等于一个值,但
object1可能为null。

目前我有这样的代码:

if(object1!= null)
{
if(object1.Property == desiredValue)
{
//在这里工作
}
}

这是我的问题 - 我可以这样做吗......
if(object1!= null&& object1.Property == desiredValue)
{
//在这里工作
}

....确保条件总是从左到右评估,并且一旦条件评估为假,评估就会停止?

如果我可以将条件合并到一个if语句中,你认为<这或多或少是优雅的编码?

比尔
Hi There

Say I want to check if object1.Property1 is equal to a value, but
object1 could be null.

At the moment I have code like this:

if (object1 != null)
{
if (object1.Property == desiredValue)
{
//Do work here
}
}

Here is my question - Can I do this ...

if (object1 != null && object1.Property == desiredValue)
{
//Do work here
}

.... and be sure that conditions are always evaluated left to right and
the evaluation stops as soon as a condition evaluates to false ?

If I can collapse the conditions into one if statement, do you think
this is more or less elegant coding?

TIA
Bill



谢谢Marinus

Thanks Marinus


此外,nte在原始中指定了此行为nal C语言

定义,并转入C ++,Java和C#。这对于一个特定的编译器来说并不是特别的,但是要求任何编译器实现

任何一个C系列语言。


-

-

真相,

James Curran

[以前的VC ++ MVP]


主页: www.noveltheory.com 工作: www.njtheater.com

博客: www.honestillusion.com 日工作: www.partsearch.com

<或******* @ yahoo.com.au>在消息中写道

news:11 ********************** @ g47g2000cwa.googlegr oups.com ...
Also, nte that this behavior was specified in the original C language
definition, and carried over into C++, Java and C#. This is not particular
to one specific compiler, but a requirement for any compiler implementing
any of the C family of langauges.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

<or*******@yahoo.com.au> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
你好

说我想检查object1.Property1是否等于一个值,但
object1可能为null。

目前我有这样的代码:

if(object1!= null)
{
if(object1.Property == desiredValue)

//在这里工作
}
}

这是我的问题 - 我可以这样做吗......

if(object1!= null&& ; object1.Property == desiredValue)
{
//在这里工作
}

...并确保条件始终从左到右评估
一旦条件评估为假,评估就会停止?

如果我可以将条件折叠成一个if语句,你认为这或多或少是优雅的编码吗?

TIA
Bill
Hi There

Say I want to check if object1.Property1 is equal to a value, but
object1 could be null.

At the moment I have code like this:

if (object1 != null)
{
if (object1.Property == desiredValue)
{
//Do work here
}
}

Here is my question - Can I do this ...

if (object1 != null && object1.Property == desiredValue)
{
//Do work here
}

... and be sure that conditions are always evaluated left to right and
the evaluation stops as soon as a condition evaluates to false ?

If I can collapse the conditions into one if statement, do you think
this is more or less elegant coding?

TIA
Bill



这篇关于(cond1&amp;&amp; cond2&amp;&amp; cond3)总是从左到右评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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