2

While we're all familiar with:

switch(x){ case 1: do_something(); break; case 2: do_something_else(); break; default: do_default(); break; } 

I was wondering if there exists in any language a syntactic variation like this:

switch(x){ case(1){ do_something(); } case(2){ do_something_else(); } default{ do_default(); } } 

As I write this, I realize that there is no clean way to indicate a certain case is non-breaking, and that program flow should cascade into subsequent cases. Likely that is reason enough, but I was always curious as to why C family languages (I come from PHP) that I've seen in passing abandon the brace syntax for switch construct case statements.

4
  • In C/C++/Java the switch branches fall through. However, in C# it is not permissible that the branches fall through so, while there are no braces in the C# switch syntax either, the lack of fall-through removes any syntactical advantage -- except perhaps omitting a "case terminator". Commented Apr 5, 2011 at 20:01
  • Thanks pst; I recall having read this also when I was familiarizing myself with C#, some time ago. In this instance, while not permitted, it would not be of any disadvantage to allow this syntax. Out of curiosity (I'll be Googling in the meantime) why is it that C# does not permit fall-through? Commented Apr 5, 2011 at 20:05
  • Was my answer unhelpful? It seemed like a good example of the syntax you were describing. Commented May 11, 2011 at 11:43
  • @Mr.Wizard; Sorry, it certainly was, however the example eldarerathis gave was much closer to what I was interested in. The syntactic deviation of switch has always piqued my curiosity in "brace-block" languages. +1 for bringing my attention back to it; I'm learning C and as an educational project, I'm writing an interpreter for a simple scripting language I'm creating. Any information is good information! Commented May 11, 2011 at 15:06

7 Answers 7

2

Not a C-like language, but Perl's given/when syntax actually looks like what you're describing. From the docs:

use feature ":5.10"; given($foo) { when (undef) { say '$foo is undefined'; } when ("foo") { say '$foo is the string "foo"'; } when ([1,3,5,7,9]) { say '$foo is an odd digit'; continue; # Fall through } when ($_ < 100) { say '$foo is numerically less than 100'; } when (\&complicated_check) { say 'a complicated check for $foo is true'; } default { die q(I don't know what to do with $foo); } } 
Sign up to request clarification or add additional context in comments.

6 Comments

That's a different beast, though; doesn't given-when require you to explicitly write continue in order to fall-through? Or do I have that backwards?
@Tomalak: Yes, it's the opposite of most C-style switch statements. You have to explicitly continue to avoid break-ing on a given condition. That seemed to me like what the OP was implying, though, based on the fact that he left the explicit breaks out of his second example but seemed to equate them to each other (i.e. for his first example to have the same flow as the second example with the braces, each block in the second must have an implicit break).
Thanks eldarerathis; Very interesting. I've been passively working my way to choosing a new language to look at (from PHP to Perl/Python/Ruby) and while others have been factoring in quite heavily for application, Perl has quirky constructs and I like that. And spot on with your assumption of my second example; implicit breaks were by accidental design :P
@eldarerathis: Then this answer is not about switch cases. o.O
@Tomalak: I'm not quite sure what you mean. The question said "I was wondering if there exists in any language a syntactic variation like this: [code]" and I wanted to show that Perl's syntax looks almost identical to what he posted.
|
1

I don't know of any language that supports the second method you illustrate, however C-based languages support a similar syntax where you can create an anonymous code block within your case statement, like so:

switch(x){ case(1): { do_something(); } break; case(2): { do_something_else(); } break; default: { do_default(); } } 

This is good if you want to scope variables within your cases. Of course, you still need the break to avoid falling through.

2 Comments

If i found some piece of code with completely superfluous parentheses as in “case(1):”, i would guess that the programmer didn't learn the language properly and wrote it like that “just to be safe”. I'd remove the parentheses right away (this is not applicable to the braces, since they create a new scope and removing them could change the meaning of the program).
I only included them because the OP did - he specifically asked for that syntax.
1

In C all the braces do is define the scope (where they can be accessed by the code) of variables, it is not used for flow control. The only clean way to indicate that case fall-through is deliberate is to comment it

switch (a) { case 0: doSomethingSpecial(); // Deliberate fall-through case 1: doSomething(); break; case 2: doSomethingElse(); break; } 

Comments

1

There is no language I'm aware of that requires braces as part of the switch/case syntax. It wouldn't necessarily make much sense, as braces typically introduce "block scope", which is conventionally not in play between case statements.

It may interest you to know, however, that in some languages (like C++) you can use braces as part of the case statement (rather than as part of the switch/case syntax itself) to introduce your own bounded scope:

switch (x) { case a: { ... break; } } 

Sometimes a C++ compiler will require you to do this, most notably when you declare variables inside a case statement.

Comments

1

This wikipedia reference discusses the switch / case construct and immediately below it is the functional programming paradigms specification of pattern matching.

To quote, "Pattern matching may be seen as a more sophisticated alternative to both if-then-else, and case statements."

1 Comment

switch (in C/C++/Java, but not C#) has a fall-through behavior generally not found in either if-then-else or pattern-matching constructs though. It also generally has a restriction of the value being compared and this restriction (e.g. for small numbers) may be used to create a direct jump table by compilers.
1

Mathematica uses the following switch syntax:

Switch[ x, 1, action[1], 2, action[2], _, defaultaction[] ] 

That is, an expression x, then an alternating sequence of pattern and action. The token _ is a pattern object representing any expression, and therefore defines the default.

Comments

0

In Common Lisp case constructs (case, ccase, ecase) each different option is handled in a single separate s-expr; there is no need of "break" and after the labels there is an implicit progn so multiple forms are allowed.

There's no fall-through option however...

(case x ((1 2 3) (do-this)) ((4 5) (do-that)) (otherwise (do-something-else))) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.