Why do we need break after case statements?

后端 未结 17 2250
猫巷女王i
猫巷女王i 2020-11-22 04:34

Why doesn\'t the compiler automatically put break statements after each code block in the switch? Is it for historical reasons? When would you want multiple code blocks to e

17条回答
  •  不知归路
    2020-11-22 04:41

    So you do not have to repeat code if you need several cases to do the same thing:

    case THIS:
    case THAT:
    {
        code;
        break;
    }
    

    Or you can do things like :

    case THIS:
    {
       do this;
    }
    case THAT:
    {
       do that;
    }
    

    In a cascade fashion.

    Really bug/confusion prone, if you ask me.

提交回复
热议问题