Why don't switch statements have curly braces?
Asked Answered
D

2

5

The typical syntax for a switch-statement is:

switch(expression) {
  case one:
    // do something...
    break;
  case two:
    // do something else...
    break;
  case three:
  case four:
    // do something special
    break;
  default:
    // if nothing matches...
    break;
}

This syntax looks very Python-like (especially the colons and the way people indent it), contrary to the general syntax of languages such as JavaScript, Java, or C, where curly braces are used for most statements. Is there any specific reason?

I would expect that it should look more like:

switch(expression) {
  case (one) {
    // do something...
    break;
  } case (two) {
    // do something else...
    break;
  } case (three) case (four) {
    // do something special
    break;
  } default {
    // if nothing matches...
    break;
  }
}

I think the example I just provided looks like an if-else if-else statement, but shouldn't a switch statement match the syntax style?

Declare answered 17/12, 2021 at 2:57 Comment(14)
@Pointy yes, but why is it optional?Declare
Think of each case as a label for a goto. But there is no goto, instead the switch is jumping to the label.Enure
C was the original they all inherited it from. See https://mcmap.net/q/2034052/-who-invented-the-switch-statement-closed for more details.Metritis
Perhaps you should ask why don't other languages look more like C ;-) BTW, just to confuse you further :-), you can do: switch (expression) { case one: { // do something } break; }Highsmith
C copied the syntax from an early version of BCPL. See C history.Enure
When switch statement found matching case, every code below that case executed until meet break. Syntax what you suggest seems run only matching case.Salicylate
Questions about why languages are the way they are often have useful answers. Languages don't spring out of nothingness, or by rolling dice. It is perfectly reasonable to ask why they are designed the way they are. Closing this as "not reproducible or caused by typos" is a misuse of the close system.Chamber
@Salicylate I'm just wondering about the syntax, not the functionality.Declare
Why do 'if' statements have curly braces (in the general case) and not the much better if-then-elif-else-fi style? Same answer: a liking for C syntax by Java designers.Wizardly
I guess because they figured programmers would want fall-through. Of course, then there's the question of whether or not it causes more problems than it solves (accidental fall-through because one forgets to break;). The C# people thought so, so they only let you do it if you explicitly use goto nextLabel;.Aufmann
@Wizardly Okay, Java likes C. Where did C get the idea from?Declare
The curious case of the switch statementAkira
You can really abuse a switch statement if you're creative. Check out Duff's device.Joo
A lot of things in C simply don't make sense and there is no rationale for them. switch statements in particular have a rather weird and brittle syntax.Recoil
R
5

case statements are actually treated as labels in C, similar to those used by goto. Why it was designed like that is hard to answer and involves digging through various old publications by Dennis Ritchie.

More importantly for present day, you can use the switch with braces and it's even considered good practice. This is valid C (and C++):

switch(expression) 
{
  case one:
  {
    // do something...
    break;
  }
  case two:
  {
    // do something else...
    break;
  }
  case three:
  case four:
  {
    // do something special
    break;
  }
  default:
  {
    // if nothing matches...
    break;
  }
}

Now you can even declare local variables inside each case.

Recoil answered 17/12, 2021 at 7:41 Comment(0)
S
2

In C-like programming languages, an identifier with a colon used as label to jump.

label:   
// ...
goto label;  

JavaScript doesn't have a goto statement but still has labels.

label1: {
  label2: {
    console.log('break nested block');
    break label1;
    console.log('unreachable');
  }
  console.log('unreachable');
}

In switch statement, each case has a label for it. And switch statement has its own list for those labels called jump table. When given value is matching, jump to that label and execute every code follows like goto.

Salicylate answered 17/12, 2021 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.