홍익인간 프로젝트/C

'break' statement when using curly braces in switch-caseAsked

Remoted remoted 2021. 3. 4. 14:19

42

4

I use curly braces with all of my switch case statements in C/Objective-C/C++

I had not, until a few moments ago, considered whether including the break; statement inside the braces was good or bad practice. I suspect that it doesn't matter, but I figure it is still worth asking.

 

 

 

 

 

Just a give a slightly more detailed answer...

The official C99 specification says the following about the break statement:

A break statement terminates execution of the smallest enclosing switch or iteration statement.

So it really doesn't matter. As for me, I put the break inside the curly braces. Since you can also have breaks in other places inside your curly braces, it's more logical to also have the ending break inside the braces. Kind of like the return statement.

 

 

 

1 2 3 4