Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Break inside case brackets in a switch statement

I'm curious about inserting the break inside a brackets in a case.

  int a = 1;
    switch (a)
    {
    case 1:
    {
        a = a;
        break;
    }
    a = a;
    case 2:
    {
        a = a;
        
    }
    break;
    }

In the C++ code above, in case 1 I've inserted the break inside the brackets, it is an error?

Is there any difference between case 1 and case 2 in the example above?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Is there any difference between case 1 and case 2 in the example above?

In terms of the execution of the code and the behaviour of the break statement – no. From this Draft C++17 Standard:

9.6.1 The break statement        [stmt.break]

1    The break statement shall occur only in an iteration-statement or a switch statement and causes termination of the smallest enclosing iteration-statement or switch statement; control passes to the statement following the terminated statement, if any.

So, any 'extra' enclosing scopes (blocks delimited by { ... }) are irrelevant: the break will operate on the first switch (or iteration statement) the compiler finds in an 'outward search' from where it is placed. (Note that the same is true for a break statement inside the scope of an if block: that will still break out of the enclosing switch block or while/for loop.)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda