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

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


当前回答

我知道这是一个老帖子,但我觉得这真的需要说,没有其他地方可以说。这里的每个人都使用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.].)

其他回答

我的建议是使用一个检查变量来打破一个期望的循环。结果代码可能不那么令人愉快。 您可以使用预处理器,以便在引擎盖下进行所需的破坏。这种方法可以隐藏丑陋的代码和额外的复杂性。 例如,我创建了自定义中断机制,如下所示:

想要的代码:

for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
        for (int k = 0; k < 100; k++) {
            //do something
            if (desiredCondition) {
                breakToLevel = 0;
            }
            if (breakToLevel < 3) {
                break;
            }
        }
        if (breakToLevel < 2) {
            break;
        }
    }
    if (breakToLevel < 1) {
        break;
    }
}

宏定义:

#define BREAK_TO(L) breakToLevel = (L); 
#define CHECK_BREAK(L) if (breakToLevel < (L)) break; 

和结果:

for (int i = 0; i < 100; i++) {
    for (int j = 0; j < 100; j++) {
        for (int k = 0; k < 100; k++) {
            //do something
            if (desiredCondition) {
                BREAK_TO(0)
            }
            CHECK_BREAK(3)
        }
        CHECK_BREAK(2)
    }
    CHECK_BREAK(1)
}

Break将只退出包含它的最内层循环。

你可以使用goto来跳出任意数量的循环。

当然,goto通常被认为是有害的。

使用break函数[…]是否合适?

使用break和goto会使判断程序的正确性变得更加困难。请看这里关于这个问题的讨论:Dijkstra不是疯子。

 bool found = false;
    
    for(int i=0; i < m; ++i){
        for(int j=0; j < n; ++j)
            if(grid[i][j] == '*'){
                q.push(make_pair(i,j));
                found = true;
                break;
            }
        if(found)
            break;
    }

一个使用goto和标签跳出嵌套循环的代码示例:

for (;;)
  for (;;)
    goto theEnd;
theEnd:

我认为goto在这种情况下是有效的:

要模拟中断/继续,你需要:

打破

for ( ;  ;  ) {
    for ( ;  ;  ) {
        /*Code here*/
        if (condition) {
            goto theEnd;
        }
    }
}
theEnd:

继续

for ( ;  ; ) {
    for ( ;  ;  ) {
        /*Code here*/
        if (condition) {
            i++;
            goto multiCont;
        }
    }
    multiCont:
}