Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to put break in switch/case statement with blocks

Tags:

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.

like image 917
jackhab Avatar asked Jul 23 '09 11:07

jackhab


People also ask

How do you use a break statement in a switch case?

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.

What keyword is used to break a switch block?

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.

Do you need breaks in switch statements?

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.

Does the last case in a switch statement Need a break?

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.


1 Answers

It's a matter of style.

I would put break outside the closing brace just to make it more readable.

like image 165
Indy9000 Avatar answered Oct 11 '22 16:10

Indy9000