Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!