用 return .. 和默认情况下打破 [英] break in a case with return.. and for default

查看:22
本文介绍了用 return .. 和默认情况下打破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的强迫症让我在编写 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;
}

我的两个问题是:
对于案例 2:",我真的不需要休息,但无论如何把它放在那里是个好主意吗?对于默认:".是纯粹的强迫症,还是有什么真正的理由在这里休息?

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?

推荐答案

你不需要任何一个 break,但是拥有它们并没有什么坏处.在我看来,保持你的代码结构化是值得的.

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.

这篇关于用 return .. 和默认情况下打破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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