在 C++ 中使用“else if”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12757653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:35:22  来源:igfitidea点击:

use of "else if" in c++

c++if-statement

提问by SegFault

I have two questions - (I)

我有两个问题 - (I)

code-fragment-1

if(<condition-statement>){
}

else if(<condition-statement-2>){
    //statements-1
}
//statements-2

code-fragment-2

代码片段 2

if(<condition-statement>){
}

else{
    if(<condition-statement-2>){
        //statements-1
    }
    //statements-2
}

Are the above two code fragments same?

上面两个代码片段一样吗?

(II) when are else ifs (in C++) used?

(二)else ifs(在C++中)什么时候使用?

回答by Adam Plocher

The only difference is in example 1 your Statement2 will get executed regardless of the conditions you check. In example 2, Statement2 will only get executed if your ifcondition is false. Other than that, they're basically the same.

唯一的区别是在示例 1 中,无论您检查什么条件,都将执行 Statement2。在示例 2 中,只有在您的if条件为假时才会执行 Statement2 。除此之外,它们基本相同。

回答by obe6

No, in the first case you execute the else block only if the <condition-statement>is not verifiedAND only if<condition-statement-2>is verified.

不,在第一种情况下,你执行else块只有在<condition-statement>没有被证实<condition-statement-2>被验证

In the second case you execute the else block simply if the <codition-statement>is not verified.

在第二种情况下,你执行else块简单,如果<codition-statement>未验证

In this case are equivalent untilyou does not have any //statements-2.

在这种情况下是等价的,直到您没有任何 //statements-2。

About the question : when is the else if (in c++) used ?

关于问题:何时使用 else if(在 C++ 中)?

Is used basically under the same conditions of all other languages?? that have this construct. elseis executed as alternative to the related if, else-ifis executed as alternative but with an 'attached' ifto be verified, otherwise is not executed. So they are not logically equivalent.

是不是在使用其他所有语言的条件基本相同??有这个构造。 else作为相关 if 的else-if替代执行,作为替代执行但带有if要验证的“附加” ,否则不执行。所以它们在逻辑上不是等价的。

回答by Avery3R

the syntax of an if is really if(condition) statement;

if 的语法实际上是 if(condition) 语句;

What the {} really do is allow you to group together multiple statements. In your second example you only have one statement(the if) inside your {}s, so yes, both examples are the same, except //statements-2 always gets run when !=true

{} 的真正作用是允许您将多个语句组合在一起。在您的第二个示例中,您的 {} 中只有一个语句(if),所以是的,两个示例都是相同的,除了 //statements-2 总是在 !=true 时运行

回答by user207421

In your first code sample, statement-2 is executed unconditionally. In the second it is conditional. Not the same.

在您的第一个代码示例中,语句 2 是无条件执行的。第二种是有条件的。不一样。

'else if' is generally to be preferred, because you can keep inserting or appending more of them indefinitely, or append an 'else', whereas with the other form you have to endlessly mess around with braces to get the same effect, and you risk altering the semantics, as indeed you have done in your second sample.

'else if' 通常是首选,因为您可以无限期地继续插入或附加更多的它们,或者附加一个 'else',而对于另一种形式,您必须无休止地使用大括号来获得相同的效果,并且您冒着改变语义的风险,正如您在第二个示例中所做的那样。