20

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?

8
  • 4
    C# requires that you be explicit with goto case, so the premise of your question is a bit wrong.
    – pdr
    Commented Aug 28, 2012 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
    Commented Aug 28, 2012 at 14:00
  • 1
    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
    Commented Aug 28, 2012 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
    Commented Aug 28, 2012 at 14:47
  • Imagine the switch is inside a loop. Then continue is ambiguous because the intention may have been to jump to the next loop iteration. Also if you add continue with this semantics in an existing language, you'll break lots of code. Commented Mar 31, 2021 at 15:02

2 Answers 2

23

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.

3
  • 10
    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
    Commented Aug 28, 2012 at 14:26
  • 3
    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
    Commented Aug 28, 2012 at 14:59
  • 1
    @zxcdw: Too bad there's no way a little birdy could go back in time and suggest that any label marked case, other than the first, should be automatically prefixed with break, but those marked +case (or some other such designator) should not. That would have been easy for a compiler to handle and allowed the semantic advantages of the present arrangement, while eliminating many lines of code.
    – supercat
    Commented Jul 14, 2015 at 18:18
9

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.

1
  • 1
    Using a new keyword like fallthrough for C would have been a great idea also because break really has two meanings in C: one for breaking out of loops (do, while, for) and one for getting out of a switch statement. There are situations were you'd like to break out of a loop in the middle of a switch but you cannot because of this.
    – SO Stinks
    Commented May 7, 2020 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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