在if(表达式)中排序 [英] order in if(expression)

查看:97
本文介绍了在if(表达式)中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有
如果(i == 1&& func1())做某事


if i!= 1 ,func1根本没有运行

这是在我的编译器中


这是因为编译器还是因为stantard

我可以依赖于任何系统func1将永远不会运行如果我!= 1

(所以首先检查第一个文件)


感谢

解决方案



Babis Haldas写道:

我有

如果(i == 1&& func1())做某事

如果我!= 1,func1根本不运行
这是在我的编译器中

这是因为编译器或因为标准
我可以依赖于任何系统func1永远不会运行如果我!= 1
(所以首先检查第一个文件)




这种行为是标准的。你可以信赖它。如果左侧是&b;&b;&&结果是假的,右手边根本没有评估。


逻辑或运算符||表现相似。如果左侧

的评估结果为true,则不评估右侧。


Gavin Deane


>如果(i == 1&& func1())做某事


如果你需要运行func1,请将值1调整,然后这个表达式

不适合你,因为如果我与1不同,那么包含fun1的表达式将不会被评估。


它被认为是穷人风格依赖于

表达式的评估顺序,因为它可能会导致代码读者混淆。它是一个更好的想法,以获得函数的方式重写代码

调用外部表达式以使其更清晰。




Babis Haldas写道:

我有

if(i == 1&& ; func1())做点什么

如果我!= 1,func1根本不运行
这是在我的编译器中

这是因为编译器还是因为标准
我可以依赖任何系统func1永远不会运行如果我!= 1
(首先检查第一个文件)

谢谢



&&和||被称为快捷运算符。 &安培;&安培;只有在&&和&& ;;左右两侧的表达式为

时才返回TRUE。运算符为TRUE。在

你的情况下,如果''i!= 1''则没有必要用右手

表达方面。


所以基本上你可以依赖标准并且总是假设在

的情况下&&如果第一个表达式为FALSE,则右侧将无法计算



同样,在||的情况下如果第一个表达式为TRUE,则第二个

表达式(在右侧)将不会计算,因为||返回

即使其中一个表达式为TRUE,也为TRUE。


i have

if(i==1 && func1() ) do something

if i!=1 , func1 doesnt run at all
this is in my compiler

is this because the compiler or because the stantard
can i rely that in any system func1 will never run if i!=1
(so first agument is checked first)

thanks


解决方案


Babis Haldas wrote:

i have

if(i==1 && func1() ) do something

if i!=1 , func1 doesnt run at all
this is in my compiler

is this because the compiler or because the stantard
can i rely that in any system func1 will never run if i!=1
(so first agument is checked first)



That behaviour is standard. You can rely on it. If the left hand side
of && eavluates to false, the right hand side is not evaluated at all.

The logical-or operator || behaves similarly. If the left hand side
evaluates to true, the right hand side is not evaluated.

Gavin Deane


> if(i==1 && func1() ) do something

If you need func1 to run, dispite the value of 1, then this expression
will not work for you, since if i is different from 1, the expression
containing fun1 will not be evaluated.

It is considered poor style relying on the evaluation order of
expressions since it may cause confusion on the reader of your code. It
is a better idea to rewrite your code in a way to get the function
calls outside expressions in order to make it clearer.



Babis Haldas wrote:

i have

if(i==1 && func1() ) do something

if i!=1 , func1 doesnt run at all
this is in my compiler

is this because the compiler or because the stantard
can i rely that in any system func1 will never run if i!=1
(so first agument is checked first)

thanks



&& and || are called shortcut operators. && returns a TRUE only if
the expression on left and right side of the && operator is TRUE. In
your case, if ''i !=1'' then there is no point in doing the right hand
side of the expression.

So basically you can rely on the standard and always assume that in
case of && if the first expression is FALSE the right hand side would
not be calculated.

Similarly, in-case of || if the first expression is TRUE the second
expression (on the right side) would not be calculated since || returns
a TRUE even if one of the expressions are TRUE.


这篇关于在if(表达式)中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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