在Java中,break如何与嵌套循环交互?

发布于 2024-10-19 07:20:47 字数 60 浏览 6 评论 0原文

我知道 break 语句会跳出循环,但它是跳出嵌套循环还是仅跳出当前所在的循环?

I know a break statement jumps out of a loop, but does it jump out of nested loops or just the one its currently in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

燃情 2024-10-26 07:20:47

如果没有任何修饰,break 将直接跳出最内层的循环。因此,在这段代码中:

while (true) { // A
    while (true) { // B
         break;
    }
}

break仅退出循环B,因此代码将永远循环。

然而,Java 有一项称为“命名中断”的功能,您可以在其中命名循环,然后指定要中断的循环。例如:

A: while (true) {
    B: while (true) {
         break A;
    }
}

此代码不会永远循环,因为 break 显式离开循环 A

幸运的是,同样的逻辑适用于继续。默认情况下,Continue 执行包含 Continue 语句的最内层循环的下一次迭代,但它也可以通过指定标签来跳转到外层循环迭代。一个循环继续执行。

在Java以外的语言中,例如C和C++,这种“标记的break”语句不存在,并且不容易跳出多重嵌套循环。可以使用 goto 语句来完成,尽管这通常不受欢迎。例如,假设您愿意忽略 Dijkstra 的建议并使用 goto,那么 C 中的嵌套中断可能如下所示:

while (true) {
    while (true) {
        goto done;
    }
}
done:
   // Rest of the code here.

希望这会有所帮助!

Without any adornment, break will just break out of the innermost loop. Thus in this code:

while (true) { // A
    while (true) { // B
         break;
    }
}

the break only exits loop B, so the code will loop forever.

However, Java has a feature called "named breaks" in which you can name your loops and then specify which one to break out of. For example:

A: while (true) {
    B: while (true) {
         break A;
    }
}

This code will not loop forever, because the break explicitly leaves loop A.

Fortunately, this same logic works for continue. By default, continue executes the next iteration of the innermost loop containing the continue statement, but it can also be used to jump to outer loop iterations as well by specifying a label of a loop to continue executing.

In languages other than Java, for example, C and C++, this "labeled break" statement does not exist and it's not easy to break out of a multiply nested loop. It can be done using the goto statement, though this is usually frowned upon. For example, here's what a nested break might look like in C, assuming you're willing to ignore Dijkstra's advice and use goto:

while (true) {
    while (true) {
        goto done;
    }
}
done:
   // Rest of the code here.

Hope this helps!

风透绣罗衣 2024-10-26 07:20:47

默认情况下,它跳出最内层循环。但是您也可以指定标签并使其跳转外循环

By default, it jumps out of the innermost loop. But you can specify labels and make it jump of outer loops too.

抚你发端 2024-10-26 07:20:47

还可以通过使用Exceptions来突破,这样就可以处理多种原因

 void fkt1() {
    try {
        while (true)
            fkt2();
    } catch (YourAbortException e) {
        e.printStackTrace();
    }

    //go on
}

void fkt2() {
    while (true)
        if (abort)
            throw new YourAbortException();
}

You can also break out by using Exceptions, so you can handle multiple reasons

 void fkt1() {
    try {
        while (true)
            fkt2();
    } catch (YourAbortException e) {
        e.printStackTrace();
    }

    //go on
}

void fkt2() {
    while (true)
        if (abort)
            throw new YourAbortException();
}

它打破了 1 个循环。很简单。例如:

for loop
   for loop
      break;
   end for loop
end for loop

跳出内循环但仍在外循环中

it breaks 1 loop . very simple. for ex:

for loop
   for loop
      break;
   end for loop
end for loop

break out of inner loop but still in outer loop

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文