4

Like:

switch (value) {
default:
    something_common()
case 1:
    niche_case()
...
}

etcetera.

if-else statements require an "if" first and an "else" last, but switch statements are more flexible in their ordering. I think it's important to know what will happen "normally", which default indicates.

5

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.

  • 1
    Wait. Are you saying all the switch values are compared and default only called when none match? I thought each was evaluated top to bottom untill a break was hit? In which case putting default.. break; at the top would prevent any othrr case being called – Ewan May 14 '16 at 10:39
  • @ewan that's what I thought too. Especially with the ones with fallthrough behaviour like C. AFAIK in any other language you still have a short-circuiting feature that can be optimised by putting any massivly more frecuent cases at the front… – StarWeaver May 14 '16 at 11:18
  • 1
    @Ewan: how can a "default" case be evaluated except by evaluating all the other cases? In the question's example, how can we know we have a "default" case without evaluation whether "value" equals "1"? Or, the other way around, if everything is evaluated as a default case, we would never end up in the case 1: branch! – oerkelens May 14 '16 at 11:22
  • just checked the (c#) spec and doc is right (as usual) I always think of it working the old c++ way with (optional) breaks and default as a catch all. Although im not even sure that is right for c++ now!! – Ewan May 14 '16 at 11:49
  • @Ewan: "although im not even sure that is right for c++" - it is. Language designers of the mainstream languages typically don't start to change basic control-of-flow semantics in an incompatible manner, not for a really, really good reason. – Doc Brown May 14 '16 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.