Why don't languages use explicit fall-through on switch statements?
Posted
by
zzzzBov
on Programmers
See other posts from Programmers
or by zzzzBov
Published on 2012-08-28T13:43:55Z
Indexed on
2012/08/28
15:50 UTC
Read the original article
Hit count: 253
switch-statement
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?
© Programmers or respective owner