7

Possible Duplicate:
‘break’ statement when using curly braces in switch-case

While merging a package I came across this statement

switch (a)
{

case 1:
    {
        string str = "a is 1";
        cout << str << endl;
    }
    break;
case 2: ...
...

}

my question is does it matter if I place the break inside or outside the scope in case 1? here they place outside. I tried this and didn't see any difference. It makes sense to me that there is no difference but the guy with the PHD from my team said he remembers that there might be a difference but he can't remember what it is..

Community
  • 1
  • 1
Digital Da
  • 891
  • 1
  • 10
  • 23

2 Answers2

11

There is no difference whether you put the break inside or outside the scope.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

A break reached inside a switchblock causes the next statment outside this switch block to execute. Therefore, it does not matter where you place the break, inside or outside the scope.

eversor
  • 3,031
  • 2
  • 26
  • 46