假设我们有以下代码,当break时,我们想要跳出内部和外部循环,而不仅仅是内部循环,并直接进入blablabla。我们如何在 C++ 中做到这一点?
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (some condition) {
// Do something and break...
break; // Breaks out of the inner loop
}
}
}
blablabla
...
“直接去[...]”听起来像是一个完美的工作goto
。确实如此!
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (some condition) {
// Do something and break...
goto afterLoop; // Breaks out of both loops
}
}
}
afterLoop:
// More stuff
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)