简体   繁体   English

C++ 中的多个 if 语句

[英]Multiple if statements in C++

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 .我的教授最近纠正了我在另一个问题中对else陈述的看法,但我不明白为什么我必须在下一个if前面添加else 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(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 语句?

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.我认为您的实现唯一有问题的是,在数字既是 3 的倍数又是 5 的倍数的情况下,不仅两个变量都增加了,而且五项和三项变量也增加了。 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.根据教授的描述,我相信他希望您使用 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 的五和三。

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++;
   }
}

The else branch in an if-else statement is only executed if the if branch is false . if-else语句中的else分支仅在if分支为false时执行。 If you just have two if statements in a row, they'll both be executed, which can be a waste.如果你连续只有两个if语句,它们都会被执行,这可能是一种浪费。 In the below code, the else prevents the second computation from running if the first is satisfied.在下面的代码中,如果满足第一个计算,则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语句还是只应该运行一个。

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 if it would also increase both , which depending on your problem may not be what you want.在您的代码中, threesfives会增加,即使它也会增加both ,这取决于您的问题可能不是您想要的。 If you do the if/else way I've just presented, only one of the three variables is increased.如果您执行我刚刚介绍的 if/else 方式,则三个变量中只有一个会增加。

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/ - 只需用它重新格式化你的代码,你可能会有一个快乐的教授。

Why use if-else instead of multiple if's ?为什么使用if-else而不是多个if's
if-else & if would achieve the same results but if-else achieves them in a performance enhanced way. if-else & if将获得相同的结果,但if-else以增强性能的方式实现它们。 with multiple if's every if condition will have to be checked.如果有多个if's ,则必须检查每个 if 条件。 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只需执行一项条件检查,并且根本不需要检查条件的 rest。

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.这不会影响像您这样的小程序,但肯定会对可能昂贵的 function 一遍又一遍地重复调用产生一些影响。

If it works, why use switch statements ever?如果可行,为什么还要使用 switch 语句?
With nested if-else conditions the code is hard to read and understand.使用嵌套if-else条件,代码很难阅读和理解。 The switch-case construct helps to represent the conditions in a much easier to read & understand format. switch-case构造有助于以更易于阅读和理解的格式表示条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM