break in a case with return .. and for default [英] break in a case with return.. and for default

查看:148
本文介绍了break in a case with return .. and for default的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的OCD使我在写case语句时添加break,即使它们不会被执行。考虑下面的代码示例:

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

switch(option) {
    case 1:
        a = 1;
        b = 7;
        break;
    case 2:
        a = 2;
        b = 4;
        return (-1);
        break;
    default:
        a = -1;
        break;
}

我的两个问题是:

对于case 2 :,我真的不需要休息,但它是一个好主意,有它吗?
对于default:。

My two questions are:
For "case 2:", I don't really need the break, but is it a good idea to have it there anyway? For "default:". Is it purely OCD, or is there any real reason to have the break here?

推荐答案

你不需要破解,但是没有伤害他们。在我看来,保持你的代码结构化是值得有几个无关的声明。

You don't need either break, but there's no harm in having them. In my opinion, keeping your code structured is worth having a couple of extraneous statements.

这篇关于break in a case with return .. and for default的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆