Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    switch (v) {
    case FOO_A:
    case FOO_B:
      do some_foo();
      break; // if you really want :P
    case BAR_X:
    case BAR_Y:
      do_some_bar();
    }
Here you have two implicit fallthroughs used for cleaner code.

Another use is to express one case as a subset or superset of another case:

    switch (v) {
    case FOO_BAR:
       do_some_foo();
    case BAR:
       do_some_bar();
    /*...*/
    }
IIRC, there was some C-like programming language out there (C# probably?) which only provided default fallthrough if there was no code between the cases, as in the above example.


You're defending against a different claim. The idea is not that C's current way of doing things has no advantages, the idea is that it's not the optimal way. Pointing out a case in which it happens to initially be more helpful is basically a no-op. And I can think of better ways to spell even what you have there. Consider a "cases FOO_A, FOO_B" construct or something that both makes explicit what you are trying to do and still retains default break behavior. Fewer characters, clearer semantics, not hard to format for easy reading. C's way is definitely not optimal even on its own terms.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: