-4

First of all, I know breaks fix this issue, but I just want to make sure I understand the default behavior of a switch in C++.

I have the following C++ code that isn't working the way I think it would coming from other languages:

#include <iostream>

using namespace std;

int main(){
    int n;
    cin >> n;
    switch(n) {
    case 1: cout << "one" << endl;
    case 2: cout << "two" << endl;
    case 3: cout << "three" << endl;
    case 4: cout << "four" << endl;
    case 5: cout << "five" << endl;
    case 6: cout << "six" << endl;
    case 7: cout << "seven" << endl;
    case 8: cout << "eight" << endl;
    case 9: cout << "nine" << endl;
    default: cout << "Greater than 9" << endl;
    }
    // your code goes here
    return 0;
}

Which outputs:

$ ./a.out 
1
one
two
three
four
five
six
seven
eight
nine
Greater than 9

Why are the other cases being hit?

Why is the default behavior different from other languages?

pr6y
  • 29
  • 1
  • 7
    you need to use `break`, otherwise the `case`s fall through (which is useful in some scenarios) – UnholySheep May 17 '17 at 18:43
  • 1
    what if `n` is negative? For any negative value, your code will display **Greater than 9** – Fureeish May 17 '17 at 18:45
  • Java requires breaks. What language are you saying doesn't need breaks? – takendarkk May 17 '17 at 18:47
  • 4
    By the way, which *"other languages"* are you referring to? Java `switch` behaves the same and in C# it would be a compile-time error (unless the `case` is completely empty) – UnholySheep May 17 '17 at 18:47
  • See [Duff's Device](https://en.wikipedia.org/wiki/Duff%27s_device) for why this behaviour can be helpful. – user4581301 May 17 '17 at 18:54
  • "Why are the other cases being hit?" Because that's how `switch` works in C++. A `case` without a `break` falls through to the next `case`. Just the way the language works. – Jesper Juhl May 17 '17 at 18:58
  • See also; [How to Get Fired Using Switch Statements & Statement Expressions](http://blog.robertelder.org/switch-statements-statement-expressions/) . – Jesper Juhl May 17 '17 at 19:00
  • 1
    "default: cout << "Greater than 9"" not really. It *might* be greater, but it might also be less than `1`. – Jesper Juhl May 17 '17 at 19:12
  • This is what you are looking for http://stackoverflow.com/questions/8146106/does-case-switch-work-like-this – Sniper May 17 '17 at 19:20

2 Answers2

3

In C++, the switch statement supports something called "fall-through". This means that once one case statement has gone through and does not have a break associated with it, the code will "fall-through" to the next case and run that part too, until a break stops it or the switch ends. This means, to get your desired behaviour you have to add break to all of your case statements:

case 1: cout << "one" << endl; break;
case 2: cout << "two" << endl; break;
case 3: cout << "three" << endl; break;
case 4: cout << "four" << endl; break;
case 5: cout << "five" << endl; break;
case 6: cout << "six" << endl; break;
case 7: cout << "seven" << endl; break;
case 8: cout << "eight" << endl; break;
case 9: cout << "nine" << endl; break;
default: cout << "Greater than 9" << endl; break; // not required but good practice.
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
1

This is normal. switch in C/C++ (and many other C-like languages) fall through their case statements, from one to the next to the next until the very end.

If you don't want this (and let's be honest, almost nobody does), you need to add a break at the end of every case statement.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Blindy
  • 65,249
  • 10
  • 91
  • 131