在if(表达式)中排序

编程入门 行业动态 更新时间:2024-10-16 23:24:07
本文介绍了在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(表达式)中排序

本文发布于:2023-11-22 01:29:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!