3

cppreference.com stats that the form of the switch statement is:
attr switch ( condition ) statement
and that statement is any statement.

So, what will happen if I didn't put any case: or default: labels in statement, as:
switch (1) {std::cout << 'x';}

What if I wrap a statement that doesn't come after any case: or default: label with additional braces as:
switch (1) {{std::cout << 'x';}}
since wrapping a declaration of a variable with additional braces will make that declaration legal even if it was before every case: and default: labels.

Mason
  • 501
  • 3
  • 11

2 Answers2

4

Yes, the grammar allows any statements inside a switch statement. So the following snippet:

switch( /* condition */ ) 
{
  std::cout << "hello";
}

is perfectly valid, although useless, since statements inside a switch not preceded by a case or default label will never get executed.


As mentioned in the question, adding a scope inside a switch statement is needed if you want to declare a variable. However, this doesn't affect the above point. The following snippet:

switch( /* condition */ ) 
{
  {
    std::cout << "hello";
  }
}

doesn't execute the cout statement either.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • Thanks, I edited my question. Can you answer the additional part that I have added? – Mason Sep 18 '20 at 16:27
  • @cigien: There is an exception: loops can _loop_ back into the code before the first case. Aka [Duff's Device](https://en.wikipedia.org/wiki/Duff%27s_device), which can work wonders in micro-optimizations in some cases. – Mooing Duck Sep 18 '20 at 16:35
  • @MooingDuck No, AFAIK there is no way to get control to go to a statement before a case label in a `switch`. Duff's device doesn't do that either. – cigien Sep 18 '20 at 16:37
  • 2
    @cigien http://coliru.stacked-crooked.com/a/39f7e7ee47388cf6. Duff's device does indeed loop to before the first case. It's merely that the normal form of Duff's device doesn't put code there. – Mooing Duck Sep 18 '20 at 16:39
  • @MooingDuck Wow, I had no idea that was possible. You taught me something today, thank you very much :) – cigien Sep 18 '20 at 16:41
  • 1
    @cigien: The insight for me was: `switch` is just pretty-print for `goto`. I should find out if this works in Java – Mooing Duck Sep 18 '20 at 16:41
2

Nothing happens.

[stmt.switch/5]: When the switch statement is executed, its condition is evaluated. If one of the case constants has the same value as the condition, control is passed to the statement following the matched case label. If no case constant matches the condition, and if there is a default label, control passes to the statement labeled by the default label. If no case matches and if there is no default then none of the statements in the switch is executed.

You didn't label any of your statements with a case label, so none of them is labelled by a case label that matches the condition, so no statement is executed.

Remember: labels aren't flow constructs that go between statements: they're annotations for statements. In the case of switch and goto, they're merely places to jump to. That's it.

Though the common pattern and typesetting of a switch/case example has found place in convention and in textbooks, there's nothing in the language grammar that means you have to write them that way.

switch is just a conditional goto with an optional fallback ("default") case.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35