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.
goto case
, so the premise of your question is a bit wrong. – pdr Aug 28 '12 at 13:58goto case
in C#. – zzzzBov Aug 28 '12 at 14:00goto case
, as pdr mentions. – Brian Aug 28 '12 at 14:47continue
is ambiguous whilebreak
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:15break
or not. Explicitly typing a keyword solves that ambiguity. – zzzzBov Mar 31 '21 at 20:48