Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch Without Case

Today, I accidentally discovered that my compiler doesn't complain when writing code inside of a switch statement without a case. (It did complain about the lack of case statements, but after adding one after the code, there wasn't so much as a warning to let me know the code was useless.)

I'm trying to figure out if there's a purpose to allowing the following code, or if it's just one of those things where "it's more work to restrict it, so it's allowed".

#include <iostream>
void foo() {
   std::cout << "foo" << std::endl;
}

int main()
{
   for (int a = -10; a < 10; ++a)
   {
      switch(a)
      {
         foo();
      case 4:
         std::cout << "4" << std::endl;
      }
   }
}

This now outputs "4" as expected when a == 4, and it never outputs foo. So the question is, is there some (potentially esoteric but useful) reason to allow for the statement foo(); before the first case? I know for sure I'm not allowed to declare and initialize variables there.

(FWIW, I have tested this on several compilers, and they're all yielding the same behavior. Surprisingly, they also all don't output a warning.)

like image 219
Shirik Avatar asked Aug 08 '13 18:08

Shirik


People also ask

Can we use case without switch?

switch case can be without default case. A char variable is always initialized within single quotes. The expression provided in switch should result in a constant value otherwise it will be invalid. The case values should be distinct.

What can be used instead of switch case?

Luckily, JavaScript's object literals are a pretty good alternative for most switch statement use-cases I can think of. The idea is to define an object with a key for each case you would have in a switch statement. Then you can access its value directly using the expression you would pass to the switch statement.

Can we use switch without break?

You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.

Is default in switch case necessary?

The default statement is optional and can appear anywhere inside the switch block. In case, if it is not at the end, then a break statement must be kept after the default statement to omit the execution of the next case statement.


1 Answers

Yes, the behavior is as designed in the language and you can add code in different places. Switch statements are much more complicated than the look, and they allow for quite esoteric code, whether it makes sense or not.

If you want to spend some time looking at some strange uses of switch and the location of cases, you can look at the implementation of coroutines in the boost asio library. You can write a small function with the macros, compile and see what the generated code (after macro expansion) looks like.

like image 136
David Rodríguez - dribeas Avatar answered Oct 08 '22 08:10

David Rodríguez - dribeas