Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
Why don't switch statements have curly braces?

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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda