我如何“打破” out if语句? [英] How do I "break" out of an if statement?

查看:163
本文介绍了我如何“打破” out if语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个if语句,我想打破出来。我明白,休息只是真正的循环。任何人都可以帮忙?

I have a if statement that I want to "break" out of. I understand that break is only really for loops. Can anyone help?

对于那些需要我想做的例子:

For those that require an example of what I'm trying to do:

if( color == red )
{
...
if( car == hyundai ) break;
...
}


推荐答案

嵌套ifs:

if (condition)
{
    // half-massive amount of code here

    if (!breakOutCondition)
    {
        //half-massive amount of code here
    }
}

有被冒用的风险 - 这是发生在我过去 - 我会提到另一个(不受欢迎的)是可怕的 goto ;一个break语句只是一个goto伪装。

At the risk of being downvoted -- it's happened to me in the past -- I'll mention that another (unpopular) option would of course be the dreaded goto; a break statement is just a goto in disguise.

最后,我会回应的常见情绪,你的设计可能会改进,所以大量的if语句不是必要,更不用说打破它了。至少你应该能够提取几个方法,并使用返回:

And finally, I'll echo the common sentiment that your design could probably be improved so that the massive if statement is not necessary, let alone breaking out of it. At least you should be able to extract a couple of methods, and use a return:

if (condition)
{
    ExtractedMethod1();

    if (breakOutCondition)
        return;

    ExtractedMethod2();
}

这篇关于我如何“打破” out if语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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