C++中的多个if语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6241672/
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
Multiple if statements in C++
提问by Dave2081
I'm doing the first project euler problem and I just did this
我正在做第一个项目欧拉问题,我就是这样做的
#include <iostream>
using namespace std;
int main(){
int threes =0;
int fives = 0;
int both = 0;
for (int i = 0; i < 10; i++){
if(i%3==0){
threes += i;
}
if(i%5==0){
fives += i;
}
if ( i % 5 == 0 && i % 3 == 0){
both += i;
}
}
cout << "threes = " << threes << endl;
cout << "fives = " << fives << endl;
cout << "both = " << both << endl;
cout << " threes + fives - both = " << endl;
int result = (threes + fives) - both;
cout << result<< endl;
return 0;
}
My professor recently corrected me for doing this in a different problem saying something about else statements, but I don't understand WHY i have to add else in front of the next if. for what its worth I have another version with else if(i%5){ fives += .... } and they both work and get me the right answer.
我的教授最近纠正了我在一个不同的问题中这样做的原因,即关于 else 语句的一些内容,但我不明白为什么我必须在下一个 if 前面添加 else。对于它的价值,我有另一个版本的 else if(i%5){ Fives += .... } 并且它们都可以工作并让我得到正确的答案。
My question is whats inherently wrong with this way of thinking, is it stylistic or am I not thinking logically about something?
我的问题是这种思维方式本质上有什么问题,是风格上的还是我没有逻辑地思考某事?
If it works, why use switch statements ever?
如果有效,为什么还要使用 switch 语句?
采纳答案by erjiang
The else
branch in an if-else
statement is only executed if the if
branch is false
. If you just have two if
statements in a row, they'll both be executed, which can be a waste. In the below code, the else
prevents the second computation from running if the first is satisfied.
将else
在分支if-else
,如果只执行的语句if
分支false
。如果if
连续只有两个语句,它们都会被执行,这可能是一种浪费。在下面的代码中,else
如果满足第一个计算,则阻止第二个计算运行。
if (expensive_computation1()) {
...
}
else if (expensive_computation2()) {
...
}
Additionally, it's clearer to humans reading the code whether both if
statements should be allowed to run or whether only one should.
此外,阅读代码的人类更清楚是if
应该允许两个语句运行还是只允许一个语句运行。
回答by Jonathan Rucker
The only thing that I see wrong with your implementation is that in the case where the number is both a multiple of 3 and a multiple of 5 not only is the both variable incremented but the fives and threes variables are also incremented. Based on what the professor described I believe he wants you to use an else-if so that the both variable is the only one that is incremented when you pass in a number that is both a multiple of 3 and a multiple of 5.
我认为您的实现唯一有问题的是,在数字既是 3 的倍数又是 5 的倍数的情况下,不仅两个变量都增加了,而且五和三变量也增加了。根据教授的描述,我相信他希望您使用 else-if,这样当您传入一个既是 3 的倍数又是 5 的倍数的数字时,两个变量都是唯一递增的变量。
The reason you get the correct answer both ways is because you are only going to 10 in the for loop, if you increase it to i <= 15 you will get fives and threes being 1 higher than I think he intended.
两种方式都能得到正确答案的原因是因为在 for 循环中你只会得到 10,如果你将它增加到 i <= 15,你会得到比我认为他想要的高 1 的 5 和 3。
For example:
例如:
for( int i = 0; i < 10; i++ )
{
if( ( ( i % 3 ) == 0 ) && ( ( i % 5 ) == 0 ) )
{
both++;
}
else if( ( i % 3 ) == 0 )
{
threes++;
}
else if( ( i % 5 ) == 0 )
{
fives++;
}
}
回答by DarkDust
In this case, maybe you really want this:
在这种情况下,也许你真的想要这个:
if (i % 5 == 0 && i % 3 == 0) {
both += i;
} else if (i % 3 == 0) {
threes += i;
} else if (i % 5 == 0) {
fives += i;
}
(why you do += i
instead of ++
I don't know but you didn't explain, so I just copied it)
(为什么你这样做+= i
而不是++
我不知道但你没有解释,所以我只是复制了它)
In your code, threes
and fives
were incremented even ifit would also increase both
, which depending on your problem may not be what you want. If you do the if/else way I've just presented, only one of the three variables is increased.
在你的代码,threes
并fives
分别递增,即使它也将增加both
,这取决于你的问题可能不是你想要的。如果您按照我刚刚介绍的 if/else 方式进行操作,则只会增加三个变量中的一个。
回答by malkia
To me it looks like stylistics. You can use some autoreformating tool that follows certain established stylistic look (K&R, ANSI, GNU, etc.)
对我来说,它看起来像文体。您可以使用一些遵循某些既定风格外观(K&R、ANSI、GNU 等)的自动重新格式化工具
For example astyle is such tool, http://astyle.sourceforge.net/- just reformat your code with it, and you might have a happy professor.
例如,astyle 就是这样的工具,http: //astyle.sourceforge.net/ - 只需用它重新格式化您的代码,您就可能拥有一位快乐的教授。
回答by Alok Save
Why use if-else
instead of multiple if's
?if-else
& if
would achieve the same results but if-else
achieves them in a performance enhanced way. with multiple if's
every if condition will have to be checked. With if-else
only one conditional check will have to be performed and the rest of the conditions just don't need to be checked at all.
为什么使用if-else
而不是多个if's
?if-else
&if
将实现相同的结果,但if-else
以增强性能的方式实现它们。if's
必须检查多个每个 if 条件。与if-else
只有一个条件检查将要执行和只是不需要其余的条件在所有被检查。
This would not affect a small program like the one you have but it will sure have some impact in potentially expensive function being called repeatedly over and over again.
这不会影响像你这样的小程序,但它肯定会对一遍又一遍地重复调用潜在昂贵的函数产生一些影响。
If it works, why use switch statements ever?
With nested if-else
conditions the code is hard to read and understand. The switch-case
construct helps to represent the conditions in a much easier to read & understand format.
如果它有效,为什么要使用 switch 语句?
使用嵌套if-else
条件,代码很难阅读和理解。该switch-case
构造有助于以更易于阅读和理解的格式表示条件。