Author Topic: Switch statement  (Read 4780 times)

bas

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Switch statement
« on: March 27, 2014, 09:11:44 AM »
Some new languages like C# changed the default behavior of a case statement so that it breaks by default.
I wonder if this would be an good for C2 as well. It would make writing case statements for concise, but we'd
need a keyword to specify fallthrough (fallthrough, next, ?). I currently lean to the default-break, unless there
are good arguments for default-fallthrough.

DerSaidin

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Switch statement
« Reply #1 on: April 11, 2014, 04:05:09 PM »
I'd suggest a third option: the user MUST terminate each case with a break or fallthrough (whatever keyword that may use) or return. Then there is no default, it is always explicit.

bas

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Switch statement
« Reply #2 on: April 14, 2014, 09:28:11 AM »
Nice one.
The choice would then be between more compact cases and more explicit code..

I also found another choice: In languages like Ada, there is a default break, but you
can specify more matches in single case. Something like:

switch (x) {
case 1|2: ..
case 3|4 ..
}
« Last Edit: April 14, 2014, 09:37:52 AM by bas »

kyle

  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: Switch statement
« Reply #3 on: June 20, 2014, 11:07:17 PM »
I like having multiple cases combined.  It is a fairly common thing in protocols to see several values that do the same thing (often for support of older versions of a protocol, for instance).

Best,
Kyle

chqrlie

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Switch statement
« Reply #4 on: August 29, 2015, 11:50:08 PM »
Following the principle of least surprise, DerSaidin's proposal makes a lot of sense.

The question is do we really want a new keyword such as fallthrough or fallthru?  As an alternative to a new keyword, we could use the goto keyword with new semantics:

    goto next;      /* fall thru to the code below */
    goto default;   /* to branch to the default case */
    goto case 'a';  /* to branch to the handler for 'a' */

Regarding combining multiple cases, it would be preferable to allow multiple case statements to follow one another without any intervening statement as this is the common idiom for C and it is widely used and poses no real problem.  We could also extend the syntax for the case clause, but with operators that do not clash with current use:  commas could be used to enumerate values, and a range operator could be introduced for to specify inclusive value ranges (.. seems an obvious choice for this).

DerSaidin

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Switch statement
« Reply #5 on: August 30, 2015, 03:12:25 PM »
switch (x) {
case 1|2: ..
case 3|4 ..
}

I agree with chqrlie that this would be better achieved by multiple case statements.

Code: [Select]
switch (x) {
case 1:
case 2:
  ...
  break;
case 3:
case 4:
  ..
  break;
}

(Note: imo, case 1 wouldn't require any explicit fallthrough because there is no code there to fallthrough from.)

I also agree that for the other option , would be a better separator than |.