为什么在 C++ 中使用 if-else if?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1529047/
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
Why use if-else if in C++?
提问by H4cKL0rD
Why would you use if-else statements if you can make another if statement?
如果您可以创建另一个 if 语句,为什么要使用 if-else 语句?
Example with multiple ifs:
具有多个 if 的示例:
input = getInputFromUser()
if input is "Hello"
greet()
if input is "Bye"
sayGoodbye()
if input is "Hey"
sayHi()
Example with else-if:
使用 else-if 的示例:
input = getInputFromUser()
if input is "Hello"
greet()
else if input is "Bye"
sayGoodbye()
else if input is "Hey"
sayHi()
回答by Brian Postow
If you have non-exclusive conditions:
如果您有非排他性条件:
if(a < 100)
{...}
else if (a < 200)
{...}
else if (a < 300)
....
this is very different from the same code without the "else"s...
这与没有“else”的相同代码非常不同......
回答by Michael Stum
It's also more performant.
它的性能也更高。
In your first example, everyif will be checked, even if input is "Hello". So you have all three checks.
在您的第一个示例中,即使输入是“Hello”,也会检查每个if。所以你有所有三个检查。
In your second example, execution will stop once it found a branch, so if the user types "Hello" it will be only one check instead of three.
在你的第二个例子中,一旦找到一个分支,执行就会停止,所以如果用户输入“Hello”,它将只有一个检查而不是三个。
The difference may not be much in your simple example, but imagine that you're executing a potentially expensive function and you might see the difference.
在您的简单示例中,差异可能不大,但想象一下您正在执行一个潜在的昂贵函数,您可能会看到差异。
回答by Brad Cupit
you mean like this:
你的意思是这样的:
if (a == true && b == false && c == 1 && d == 0) {
// run if true
}
if (a == false || b == true || c != 1 || d != 0) {
// else
}
An else-statement would be much clearer and easier to maintain.
else 语句会更清晰,更容易维护。
回答by Tadeusz Kopec
If you need to chose exactly oneaction from given set of actions, depending on some conditions, the natural and most clear choice is either switch (don't forget to break after each branch) or combination of if and else. When I write
如果您需要从给定的一组动作中选择一个动作,取决于某些条件,最自然和最明确的选择是 switch(不要忘记在每个分支后中断)或 if 和 else 的组合。当我写
if (conditon1)
{
action1();
}
else if (condition2)
{
action2();
}
else if (conditon3)
{
action3();
}
.
.
.
else {
action_n();
}
it is clear to the reader that exactly one of actions is to be performed. And there is no possibility that because of mistake in conditions more than one action is performed.
读者很清楚,只需要执行一项操作。并且不可能因为条件错误而执行不止一个动作。
回答by Uzair Nadeem
Following your same example if we use sequence of if
conditions, whatever the input is it will run all 3 conditions. Replacing sequence of if
with if-else
conditions will run only first condition in best case whereas all 3 in worst case.
如果我们使用if
条件序列,则遵循相同的示例,无论输入是什么,它都会运行所有 3 个条件。在最好的情况下,sequence of if
用if-else
条件替换只会运行第一个条件,而在最坏的情况下,所有 3 个条件都将运行。
So conclude with that if-else
will save our running time in most cases, therefore using if-else
is preferred over using sequence of if
conditions.
因此得出结论,if-else
在大多数情况下将节省我们的运行时间,因此使用if-else
优先于使用sequence of if
条件。
input = getInputFromUser()
if input is "Hello"
greet()
if input is "Bye"
sayGoodbye()
if input is "Hey"
sayHi()