Putting the default
statement first will work technically, but is a very unusual idiom. IMHO that's because what you scetched above is not the typical "use case" for a switch/default
block. It is much more common to use the default
branch similar to an else
statement, like
switch (value) {
case 1:
potential_case1()
case 2:
potential_case2()
case 3:
potential_case3()
default:
remaining_cases()
...
}
(for the sake of brevity, I left out the break
statements intentionally).
In fact, the keyword default
might give you a different association, but don't forget these terms were defined in the C language several decades ago and inherited by lots of other languages like C++, JavaScript, Java, C# or PHP - noone will change that now for historical reasons and reasons of backwards compatibility.
In some other languages, where the language creators did not follow a "similar control-of-flow terms as in C" goal, the default
section is named in a manner which expresses the intention of beeing an "else" branch more clearly, like in VB.NET, where it is called Case Else
, or Perl
, where indeed the term else
is used.