2

I'm starting to grasp c++ but there is one thing that confuses me and it's using break and select case. I know how to do this but what I would like to understand is why this certain operation happens.

Say if i have

switch (Tasty)
{
  case true:
    cout << "yum" << endl;
    break;
  case false:
    cout << "erch" << endl;
  break;
}

Now that does it correctly and prints out what I want, but if I do

switch (Tasty)
{
  case true:
    cout << "yum" << endl;
  case false:
    cout << "erch" << endl;
}

Why does it print both "yum" and "erch"?

bolov
  • 72,283
  • 15
  • 145
  • 224
  • 1
    Without a `break`, it will "fall through" to the next `case`. – crashmstr Feb 25 '14 at 17:19
  • 1
    Read this previous SO answer for [fall through reason for break statement](http://stackoverflow.com/a/252733/1079907). – Sunil Bojanapally Feb 25 '14 at 17:20
  • Note that for boolean values, it makes more sense to use `if(Tasty) ... else ...` which is in your case `if(Tasty) { cout << "yum" << endl; } else { cout << "erch" << endl; }` – leemes Feb 25 '14 at 17:20
  • 1
    Another option would be `std::cout << (Tasty ? "yum" : "erch") << std::endl;` – Fred Larson Feb 25 '14 at 17:25
  • @FredLarson I assumed that the posted code is a simplification and thus he's interested in a general solution. But of course, yours is also worth mentioning. – leemes Feb 25 '14 at 17:31

6 Answers6

8

The cases in a switch statement are best thought of as labels. After the statement

cout << "yum" << endl;

finishes running, the next one simply starts running,

cout << "erch" << endl;

unless you explicitly break out of the switch statement.

Brian Bi
  • 111,498
  • 10
  • 176
  • 312
  • It is called fallthrough http://en.wikipedia.org/wiki/Switch_statement#Fallthrough and indeed leads to bugs all the time, I've never seen a switch statement used without breaks that was useful. But I guess there are some cases where it might prevent code duplication. – Emile Vrijdags Feb 25 '14 at 17:26
5

The answers here are good, I just want to give you an example where omitting break is actually useful:

In a case you can't check for multiple values, like 1 || 2 || 3, so if you want to perform the same function for more than one value your option would be to repeat code, something like this:

switch (a)
{
  case 1:
    Foo();
    break;

  case 2:
    Foo();
    break;

  case 3:
    Foo();
    break;

  case 4:
    Bar();
    break;
}

unless you omit the break and you can write:

switch (a)
{
  case 1:
  case 2:
  case 3:
    Foo();
    break;

  case 4:
    Bar();
    break;
}

Code repetition is something to always be avoided if possible so this actually comes in handy.

bolov
  • 72,283
  • 15
  • 145
  • 224
2

If you don't use break keyword, the program will keep executing the following instructions. Sometimes this comes in handy, but most of the times it's just a pain in the ass.

In your case, if Tasty is true, both words will be printed. If false, only the second one.

Shocked
  • 627
  • 4
  • 13
1

a break is needed in most case(s) of the switch *unless you specifically want the switch to go into other cases. The switch moves down line by line regardless of break or not. Some languages like C# will automatically catch these and prevent compilation. C++ will let you call the shots however, so you need to be careful to add those in.

switch(Tasty)
{
    case true:
        cout << "yum" ;
        break; /*<--- don't forget the break!*/
    case false:
        cout << "not yum";
        break;
}
173901
  • 699
  • 1
  • 7
  • 29
  • 1
    Break is not always needed, it happens in certain cases where you don't want to break. It is true that in 99% of the cases you will put a break there because that is what you want to do, but it's not a rule. – Raxvan Feb 25 '14 at 17:21
  • I know. But in this case, OP should never forget the break. – 173901 Feb 25 '14 at 17:22
1

In switch statement cases are executing up until one is true (and then next are executing also unless you break from switch) or until all conditions have been tested.

Examine this example:

int Tasty = 1;
    switch (Tasty)
     {
         case 1:
             qDebug() << "yum" << endl;
         case 2:
             qDebug() << "erch" << endl;
     }

output:

yum

erch

int Tasty = 1;
    switch (Tasty)
     {
         case 2:
             qDebug() << "yum" << endl;
         case 1:
             qDebug() << "erch" << endl;
     }

output:

erch

4pie0
  • 29,204
  • 9
  • 82
  • 118
0

According to Cplusplus.com " It works in the following way: switch evaluates expression and checks if it is equivalent to constant1; if it is, it executes group-of-statements-1 until it finds the break statement. When it finds this break statement, the program jumps to the end of the entire switch statement (the closing brace)."

So, if you don't put break statement, it will automatically go to next case and it will keep going till it finds break statement. Follow this link for more info.

http://www.cplusplus.com/doc/tutorial/control/

thestar
  • 4,959
  • 2
  • 28
  • 22