break in a case with return.. and for default

前端 未结 13 2079
予麋鹿
予麋鹿 2020-12-11 14:19

My OCD makes me add \"break\" when writing case statements, even if they will not be executed. Consider the following code example:

switch(option) {
    cas         


        
13条回答
  •  温柔的废话
    2020-12-11 15:17

    In this exact example, for both questions, it is personal preference. In general, the rule is this: anything without a break will fall through. That means (as Pod said) its a good idea to put breaks in default cases in case they are not last. This also means if your case contains a return, then a following break is indeed not necessary.

提交回复
热议问题