1

Live version: http://cpp.sh/953y6

The code:

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int num1 = 8, num2 = 11;
    for(int n = num1; n <= num2; n++){

        if(n <= 9){
            switch(n){
                case 1: cout << "one\n";
                case 2: cout << "two\n";
                case 3: cout << "three\n";
                case 4: cout << "four\n";
                case 5: cout << "five\n";
                case 6: cout << "six\n";
                case 7: cout << "seven\n";
                case 8: cout << "eight\n";
                case 9: cout << "nine\n";
            }

      }
      else if(n % 2 == 0){ //even
            cout << "even\n";
      }
      else if(n > 9 && n %2 == 1){ //odd
            cout << "odd\n";
      }

   }

    return 0;
}

The numbers 8 through 11 are looped through on the for-loop. if(n <= 9) should only be triggered twice, when n is 8 and when n is 9. Instead, the output is:

eight
nine
nine
even
odd

Why?

Lily Potter
  • 203
  • 2
  • 8
  • your indentation is incorrect. And why do you `#include ` when you're using `iostream`? – phuclv Jan 13 '17 at 03:39
  • 1
    `case 8: cout << "eight\n"; break;` - read up on the `case` statement - the code falls through from one label to the next without the `break`. – Ken Y-N Jan 13 '17 at 03:39
  • You didn't break in the switch at all, so it starts at the one case, then execution continues through them all to the end. ... – Adam D. Ruppe Jan 13 '17 at 03:39
  • [this might be helpful](http://stackoverflow.com/questions/33069888/break-in-switch-fall-through-java) It's about the same problem in Java – haelmic Jan 13 '17 at 04:09
  • 1
    Try setting `num1` to 1 and all will be clear. – Pete Becker Jan 13 '17 at 11:45

5 Answers5

14

Because you don't have break and it's fall-through case.

case 8: cout << "eight\n";  // <-- need break here
case 9: cout << "nine\n";   // otherwise it's fall-through to here even input is 8
  • Good practice is to always break after each case of switch.
  • In case you need a fall-through intentionally then make an explicit comment about that fall-through.
artm
  • 17,291
  • 6
  • 38
  • 54
  • 1
    The fact that this hastily written, bare content answer gets so many rapid upvotes (and is now being edited to include some actual description to justify the votes, but they are already there) speaks to me as to a big part of what's wrong with Stack Overflow. Sure, it is the correct answer, but it was rushed and shallow, yet richly rewarded. – Adam D. Ruppe Jan 13 '17 at 03:43
  • @AdamD.Ruppe: What's hastily-written about it? Looks fine to me. I'd hardly call 4 upvotes richly rewarded. – GManNickG Jan 13 '17 at 03:45
  • @GManNickG it started with the bare minimum, the rest was edited in later (note that edits in the first minute also aren't shown in the history). @artm i'm more commenting on an overall trend, but your answer doesn't even explain what `break` actually does, it just gives a magic word to fix the code. The bullets added later are good, but it is still a really shallow explanation of why. – Adam D. Ruppe Jan 13 '17 at 04:08
  • @AdamD.Ruppe: The "fastest gun in the west" is not a new trend, it's been there since the start of SO. :) I agree sometimes it can be unfortunate, but it's been discussed a ton and there is no clean solution. Most of the time the non-bare answer rises to the top, if the fastest answer doesn't edit. – GManNickG Jan 13 '17 at 04:25
7

Because you dont have a break statement in your switch case :

if(n <= 9){
        switch(n){
            case 1: cout << "one\n";
            case 2: cout << "two\n";
            case 3: cout << "three\n";
            case 4: cout << "four\n";
            case 5: cout << "five\n";
            case 6: cout << "six\n";
            case 7: cout << "seven\n";
            case 8: cout << "eight\n";
            case 9: cout << "nine\n";
        }

  }

When case 8 gets called it first prints eight and then falls through to case 9 and prints nine. Then case 9 gets called when n is 9 and nine gets printed again. Add break statements after case like :

if(n <= 9){
        switch(n){
            ...
            case 8: cout << "eight\n";
                    break;
            case 9: cout << "nine\n"; //last case, dont really need a break
        }

}

Its a good idea to put break after every case in switch, unless fall through is intentional.

uptoNoGood
  • 566
  • 5
  • 20
2

In order to avoid such problem in future, you should understand the use-case of not having an implementation-defined implicit break;, at the end of each case : statement.

Consider the below example to see the benefit of "fall-through":

switch (day) {  
    case MONDAY:
    case TUESDAY:
    case WEDNESDAY:
    case THURSDAY:
    case FRIDAY:
        cout << "Weekday";  //same for all five mentioned days of the week.
        break;   // << yes, you need a explicit "break" statement to stop this fall through
    case SATURDAY:
    case SUNDAY:
        cout << "yeah! it's a Weekend";  //same for Sat and Sun.
        break;  //not necessary, but a good practice.
}
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
0

Basic rule of switch case is if you are not put break after case it will print further also. So here you didn't put break at the

Case 8 : cout << "eight\n"
//Put break here
Case 9: cout << "nine\n"

So for single print nine you must have to put break after case 8.

Dhruv Khatri
  • 803
  • 6
  • 15
0

Your code should look like this

int main() {
    // Complete the code.
    int num1 = 8, num2 = 11;
    for(int n = num1; n <= num2; n++){

        if(n <= 9){
            switch(n){
                case 1: cout << "one\n";
                break;
                case 2: cout << "two\n";
                break;
                case 3: cout << "three\n";
                break;
                case 4: cout << "four\n";
                break;
                case 5: cout << "five\n";
                break;
                case 6: cout << "six\n";
                break;
                case 7: cout << "seven\n";
                break;
                case 8: cout << "eight\n";
                break;
                case 9: cout << "nine\n";
                break;
            }
      }
      else if(n % 2 == 0){ //even
            cout << "even\n";
      }
      else if(n > 9 && n %2 == 1){ //odd
            cout << "odd\n";
      }

   }
    return 0;
}
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
waz
  • 41
  • 6