When I use braces around case
code block in C++ to localize variables should I put break
inside or outside the block?
case FOO: // 'break' inside { int i; doStuff(); break; } case BAR: // 'break' outside { int i; doStuff(); } break;
Thanks.
You can use the break statement to end processing of a particular labeled statement within the switch statement. It branches to the end of the switch statement. Without break , the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached.
Break keyword: As java reaches a break keyword, the control breaks out of the switch block. The execution of code stops on encountering this keyword, and the case testing inside the block ends as the match is found.
Technically, the final break is not required because flow falls out of the switch statement. Using a break is recommended so that modifying the code is easier and less error prone. The default section handles all values that are not explicitly handled by one of the case sections.
You don't need a break after any case label if it is the last one. Whether it is default or otherwise has nothing to do with that.
It's a matter of style.
I would put break
outside the closing brace just to make it more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With