是否可以使用break函数退出几个嵌套的for循环?

如果是,你会怎么做呢?你还能控制刹车出多少圈吗?


当前回答

打破嵌套循环的另一种方法是将两个循环分解成一个单独的函数,并在希望退出时从该函数返回。

当然,这也引发了另一个争论,即是否应该显式地从函数的任何地方返回,而不是在函数的末尾。

其他回答

你可以用try…catch。

try {
    for(int i=0; i<10; ++i) {
        for(int j=0; j<10; ++j) {
            if(i*j == 42)
                throw 0; // this is something like "break 2"
        }
    }
}
catch(int e) {} // just do nothing
// just continue with other code

如果必须同时跳出几个循环,通常会出现异常。

我知道这是一个老帖子,但我觉得这真的需要说,没有其他地方可以说。这里的每个人都使用goto。我只是用了一下。

就像几乎所有的事情一样,goto并不是100%的“非好即坏”。至少有两种用法,我想说,如果你使用goto,而不使用它,你不仅应该是100%的好,而且你的程序会比没有它更可读,因为它让你的意图更加清晰(有一些方法可以避免它,但我发现它们都更笨拙):

打破嵌套循环,和 错误处理(即在函数的末尾跳转到清理例程,以返回失败代码并释放内存。)

Instead of just dogmatically accepting rules like "so-so is 'evil'", understand why that sentiment is claimed, and follow the "why", not the letter of the sentiment. Not knowing this got me in a lot of trouble, too, to the point I'd say calling things dogmatically "evil" can be more harmful than the thing itself. At worst, you just get bad code - and then you know you weren't using it right so long as you heard to be wary, but if you are wracking yourself trying to satisfy the dogmatism, I'd say that's worse.

Why "goto" is called "evil" is because you should never use it to replace ordinary ifs, fors, and whiles. And why that? Try it, try using "goto" instead of ordinary control logic statements, all the time, then try writing the same code again with the control logic, and tell me which one looks nicer and more understandable, and which one looks more like a mess. There you go. (Bonus: try and add a new feature now to the goto-only code.) That's why it's "evil", with suitable scope qualification around the "evil". Using it to short-circuit the shortcomings of C's "break" command is not a problematic usage, so long as you make it clear from the code what your goto is supposed to accomplish (e.g. using a label like "nestedBreak" or something). Breaking out of a nested loop is very natural.

(或者更简单地说:使用goto来跳出循环。我想说那更可取。不要使用goto来创建循环。这是“邪恶的”)。

你怎么知道你是不是太教条了?如果遵循“xyz是邪恶的”规则导致您的代码不太容易理解,因为您试图绕过它(例如在每个循环中添加额外的条件,或一些标志变量,或其他类似的技巧),那么您很可能是教条主义者。

没有什么比学习良好的思考习惯更能替代良好的编码习惯。前者先于后者,一旦采用前者,后者往往紧随其后。然而,问题是,我经常发现,后者没有得到足够的解释。太多的人只是简单地说“这很糟糕”和“这需要更多的思考”,而没有说思考什么,思考什么,以及为什么。这是一个巨大的耻辱。

(FWIW, in C++, the need to break out of nested loops still exists, but the need for error codes does not: in that case, always use exceptions to handle error codes, never return them unless it's going to be so frequent that the exception throw and catch will be causing a performance problem, e.g. in a tight loop in a high demand server code, perhaps [some may say that 'exceptions' should be 'used rarely' but that's another part of ill-thought-out dogmatism: no, at least in my experience after bucking that dogma I find they make things much clearer - just don't abuse them to do something other than error handling, like using them as control flow; effectively the same as with "goto". If you use them all and only for error handling, that's what they're there for.].)

c++不支持命名循环,就像Java和其他语言一样。您可以使用goto,或创建您使用的标志值。在每个循环结束时检查标志值。如果它被设置为true,那么您可以跳出该迭代。

这个怎么样?

for(unsigned int i=0; i < 50; i++)
{
    for(unsigned int j=0; j < 50; j++)
    {
        for(unsigned int k=0; k < 50; k++)
        {
            //Some statement
            if (condition)
            {
                j=50;
                k=50;
            }
        }
    }
}

虽然这个答案已经提出了,但我认为一个很好的方法是这样做:

for(unsigned int z = 0; z < z_max; z++)
{
    bool gotoMainLoop = false;
    for(unsigned int y = 0; y < y_max && !gotoMainLoop; y++)
    {
        for(unsigned int x = 0; x < x_max && !gotoMainLoop; x++)
        {
                          //do your stuff
                          if(condition)
                            gotoMainLoop = true;
        }
    }

}