user2437820
user2437820

Reputation: 73

C++ logic with switch statements

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"?

Upvotes: 2

Views: 270

Answers (6)

bolov
bolov

Reputation: 75825

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.

Upvotes: 5

thestar
thestar

Reputation: 5249

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/

Upvotes: 0

4pie0
4pie0

Reputation: 29744

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

Upvotes: 1

173901
173901

Reputation: 709

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

Upvotes: 1

Shocked
Shocked

Reputation: 637

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.

Upvotes: 2

Brian Bi
Brian Bi

Reputation: 119392

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.

Upvotes: 8

Related Questions