The absence of brace syntax in switch cases?-Collection of common programming errors

There is no language I’m aware of that requires braces as part of the switch/case syntax. It wouldn’t necessarily make much sense, as braces typically introduce “block scope”, which is conventionally not in play between case statements.

It may interest you to know, however, that in some languages (like C++) you can use braces as part of the case statement (rather than as part of the switch/case syntax itself) to introduce your own bounded scope:

switch (x) {
  case a:
    {
       ...
       break;
    }
}

Sometimes a C++ compiler will require you to do this, most notably when you declare variables inside a case statement.

Originally posted 2013-11-09 23:32:00.