Dan Lugg
Dan Lugg

Reputation: 20612

The absence of brace syntax in switch cases?

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.

Upvotes: 2

Views: 421

Answers (7)

Mr.Wizard
Mr.Wizard

Reputation: 24336

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.

Upvotes: 1

6502
6502

Reputation: 114579

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)))

Upvotes: 0

Norman H
Norman H

Reputation: 2262

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."

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385295

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.

Upvotes: 1

James
James

Reputation: 9278

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;
}

Upvotes: 1

eldarerathis
eldarerathis

Reputation: 36213

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);
    }
}

Upvotes: 2

Tony Casale
Tony Casale

Reputation: 1537

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.

Upvotes: 1

Related Questions