Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was reading Why do we have to use break in switch?, and it led me to wonder why implicit fall-through is allowed in some languages (such as PHP and JavaScript), while there is no support (AFAIK) for explicit fall-through.

It's not like a new keyword would need to be created, as continue would be perfectly appropriate, and would solve any issues of ambiguity for whether the author meant for a case to fall through.

The currently supported form is:

switch (s) {
    case 1:
        ...
        break;
    case 2:
        ... //ambiguous, was break forgotten?
    case 3:
        ...
        break;
    default:
        ...
        break;
}

Whereas it would make sense for it to be written as:

switch (s) {
    case 1:
        ...
        break;
    case 2:
        ...
        continue; //unambiguous, the author was explicit
    case 3:
        ...
        break;
    default:
        ...
        break;
}

For purposes of this question lets ignore the issue of whether or not fall-throughs are a good coding style.

Are there any languages that exist that allow fall-through and have made it explicit?

Are there any historical reasons that switch allows for implicit fall-through instead of explicit?

share|improve this question
4  
C# requires that you be explicit with goto case, so the premise of your question is a bit wrong. –  pdr Aug 28 '12 at 13:58
1  
@pdr, I very explicitly asked if there were any languages that already supported fall-through, I was not aware of goto case in C#. –  zzzzBov Aug 28 '12 at 14:00
    
Yeah, sorry, I missed that there were two parts to your question. Unfortunately, phrased as it is, I'm voting to close because it's very close to a poll question. There will be lots of right answers. –  pdr Aug 28 '12 at 14:01
    
C# also allows you to have multiple labels share the same statement list, which removes some of the situations which require fall-through. For the rest, it has goto case, as pdr mentions. –  Brian Aug 28 '12 at 14:47

2 Answers 2

up vote 15 down vote accepted

It's primarily historical, most languages just copied what C did.

The reason that C did it that way is that the creators of C intended switch statements to be easy to optimize into a jump table. This is also the reason that C limits switch statements to integral values.

In a jump table, the program will calculate what position to jump to based on the expression. The program will jump to that point and then continue executing from that point. If you want skip the rest of the table you have to include a jump to the end of the table. C uses explicit break statements so that there is a direct correspondence to this construct.

share|improve this answer
6  
As a little note, in his book "Expert C Programming", Peter van der Linden mentions that while he was working for Sun on their C compiler, some ~97 % of the switch cases contained break and only less than 3 % were fall-through. He then used this as an example that the default fall-through behaviour is counter-intuitive and would be better off being reverse(use a keyword to indicate explicit fall-through). Oh, and the book is really great at explaining other oddities of C too, some of which are found in C++ and even in C# and Java too! It's all rooted in B and BCPL. :) –  zxcdw Aug 28 '12 at 14:26
2  
There are programming languages where fall through is explicit, like c# (msdn.microsoft.com/en-us/library/06tc147t(v=vs.71).aspx). On the other hand, breaking is also explicit in c#. –  linkerro Aug 28 '12 at 14:59

Go allows explicit fallthrough using the fallthrough keyword (break is implicit, but may be explicit):

switch val {
case 1: // breaks
case 2:
    fallthrough
case 3:
    goto 
case 4, 5, 6: // equivalent to defining individual cases with explicit fallthough
    break // unnecessary
default:
}

Here's the relevant bit from effective go and the language spec.

I don't think you can use goto to go to a specific case, but you can make a label inside the case and use a goto like normal.

As a bonus, Go lets you use binary expressions, strings, or types in a switch as case statements.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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