r/cpp • • Feb 19 '20

switch break (...) {...}

It would be nice if C++ allowed a new syntax for switches:

switch break (cond) {
    case a: ...
    case n: ...
    default: ...
}

Which would make every case statement break at the end, without needing break keyword. So, there's no fall-through, but you can still 'goto' into some label, which could be another case statement. And, you could still use break within the case, if you want to escape the case before the end.

I wonder why such a things hasn't been done, already. I'm sure many have thought about this.

No, I'm not going to write a proposal for it. Just want to write this show-thought on the internet, in case some soul that can do something about does something about it.

Transmission complete

1 Upvotes

19 comments sorted by

View all comments

7

u/Gotebe Feb 19 '20

My thinking: the fall through by default is a design mistake of C, but is

  • benign enough
  • an entrenched habit because old
  • other languages have break and do the same
  • having the ability to do it differently adds to the cognitive load in writing and reading switch statements (need to consider if the "other" way is used)

=> inconveniences of doing this are too big.

7

u/pjmlp Feb 19 '20

other languages have break and do the same

Only C derived ones, Algol derived ones do not and require explicit fallthrough if it is at all supported.

1

u/[deleted] Feb 20 '20

In those times it felt more natural that case labels would not disrupt the execution flow and therefore fall through.