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?

zzzzBov
  • 5,794
  • 1
  • 27
  • 28
  • 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
  • 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 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
  • 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. – martinkunev Mar 31 '21 at 15:02
  • @martinkunev You're saying that `continue` is ambiguous while `break` is somehow not? This problem is already solved by using labels. I wasn't making a case for updating any existing languages, but yes attempting to add this feature into an existing language would be a breaking change. – zzzzBov Mar 31 '21 at 15:15
  • @zzzzBov One of the arguments you provide against fallthrough is that it is ambiguous. I'm saying that using continue is not really an improvement in that regard. You would need some new keyword to resolve that ambiguity. The same argument goes for break but I don't see anybody arguing against break in switch. – martinkunev Mar 31 '21 at 16:45
  • @martinkunev the ambiguity that I'd referred to (9 years ago when I first wrote this) was whether the author intended to leave off a `break` or not. Explicitly typing a keyword solves that ambiguity. – zzzzBov Mar 31 '21 at 20:48

2 Answers2

24

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.

Dirk Holsopple
  • 2,701
  • 18
  • 16
  • 9
    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
  • 3
    There are programming languages where fall through is explicit, like c# (http://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
  • 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 Jul 14 '15 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.

beatgammit
  • 1,907
  • 3
  • 18
  • 25
  • 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 May 07 '20 at 11:10