Breaking from a switch

Hi everyone,

I'm still a beginner and maybe I don't see the global and real life use of switch statements, but I can't yet find a reason why a "case" statement doesn't break automatically when it drops through the bottom of its scope.
Is there a logical and practical reason to it? Do you sometimes need to pass from one case to another in the same switch statement? And wouldn't it be contratictory to the use of a switch statement if so?

(I'm talking of course about the case where a "case" drops through the bottom of its scope, not about breaking in the middle of it, in which case it is necessary)
Last edited on
I don't see the global and real life use of switch statements
switch statements are very handy when testing a state variable:
1
2
3
4
5
6
7
8
9
10
  switch (state_var)
  { 
   case 0:  // do something
               break;
   case 1:  // Do something else
               break;
   case 2:  // Do something
               break:
// etc
   }

This is a lot cleaner than a long list of if/else if statements.

I can't yet find a reason why a "case" statement doesn't break automatically when it drops through the bottom of its scope.

That's just the way it's always been since the earliest days of C.

It does have the advantage of allowing multiple cases to share the same logic:
1
2
3
4
5
6
7
8
9
  switch (state_var)
  { 
   case 0:
   case 1:  // 0 and 1 share the same logic 
               break;
   case 2:  // Do something
               break:
// etc
   }


And wouldn't it be contradictory to the use of a switch statement if so?

No.
Last edited on
@leoleoleo


Plus, you could use switch in a do/while loop, and it would continue looping until the user presses the key that escapes it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
do
{
cin >> state_var;  
switch (state_var)
  { 
   case 0:  // do something
               break;
   case 1:  // Do something else
               break;
   case 2:  // Do something
               break:
  case 3:
            cout << "Program terminated!!"<<endl;
          break;
 default:  // If none of the expected numbers were pressed
          cout << "Sorry, I don't understand what you wanted.." << endl;
   }
 }while(state_var !=3);
Last edited on
Switch/Case/Select statements are a very common and very useful idiom.

The C-style switch statement _is_ weird with the automatic fall-through. Pascal's syntax is a bit saner.

1
2
3
4
5
6
7
8
9
C and C++
switch (x)
{
  case 1:   foo(); break;
  case 2:   bar(); 
  case 3:   baz(); break;
  case 4-9: quux();
  default:  gronk();
}
Pascal

case x of
  1:    foo;
  2:    begin bar; continue end;
  3:    baz;
  4..9: quux
  else  gronk
end;

In the end, though, it doesn't really matter.

C is famous for the way it can use the switch statement syntax and interleave it with a loop. https://en.wikipedia.org/wiki/Duff%27s_device

:-@
Great, thank you for your response. The article about Duff's device was the answer I was seeking
@Duoas

cpp.sh/82ex

why is case 4-9: not working for me in cpp.sh?
why is case 4-9: not working for me in cpp.sh?

Some compilers accept the ranged case, but I don't believe it's in the standard yet.
@shadder

I believe case 4-9: was just short-hand for
1
2
3
4
5
6
7
8
9
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
  quux();
// Then the 
 default:  gronk();


meaning entering a 4 to a 9, results in quux() being called.
Yes, sorry, case 4-9 was my brain's version of:
 
  case 4: case 5: case 6: case 7: case 8: case 9:

Sorry! :-(
Topic archived. No new replies allowed.