Should we break the default case in switch statement?

后端 未结 7 1492
北荒
北荒 2020-12-15 16:51

Assuming this example code (source):

#include 

void playgame()
{
    printf( \"Play game called\" );
}
void loadgame()
{
    printf( \"Load g         


        
7条回答
  •  再見小時候
    2020-12-15 17:45

    Break Seems to Be Optional

    break seems to be optional in that case. and also your code will work almost fine.

    However

    in many situations adding break improves your code and protect your code from errors/exceptions.

    it increases readability, consistency and more error free.

    some language like C# break(or other control-flow statement that exits the case) is technically needed after the last alternative. dan04

    so for better programming practices i suggest one should use it.

    see this excellent post for more details.

提交回复
热议问题