Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 226 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'break' statement when using curly braces in switch-case

#1
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.

switch (foo) {
case 1: {
// stuff
break;
}

default: {
break;
}
}

vs

switch (foo) {
case 1: {
// stuff
} break;

default: {
// stuff
} break;
}
Reply

#2
Short answer: it doesn't matter.

<!---------------->
Reply

#3
You probably don't want the curlies in the first place unless you need them for lexical scope. The first example looks better to me, but I suppose the real answer is that it's a matter of taste.
Reply

#4
There's tons of different coding styles of how to combine curly braces and switches. I'll use the one I prefer in the examples. The `break` statement breaks out of the innermost loop or switch statement, regardless of location. You could for example have multiple breaks for a single case:

switch (foo) {
case 1:
{
if (bar)
break;
bar = 1;
...
}
break;
}

Note that you can also put the cases anywhere, though that *is* somewhat considered bad practice. The case label is very much like a goto label. It has happened that I've written something like this:

switch (foo) {
case 1:
bar = 1;
if (0) {
case 2:
bar = 2;
}
...
break;
}

but use it with care.
Reply

#5
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.
Reply

#6
As clearly stated, this is just a matter of personal style, but I always put the break statement outside the braces: putting the break before the closing brace seems to me to jump out a compound statement thus slightly increasing the spaghetti code feeling.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through