中断和标签,“标签 MyLabel 丢失”

发布于 2024-12-26 02:13:38 字数 255 浏览 3 评论 0原文

我有这样的代码:

if(condition1)
{
    break MyLabel;
}
while(true)
{
    //some code here
    MyLabel: if(condition2) break;
    //more code here
}

并且收到此错误:

标签 MyLabel 丢失。

怎么了?

I have a code like this:

if(condition1)
{
    break MyLabel;
}
while(true)
{
    //some code here
    MyLabel: if(condition2) break;
    //more code here
}

and I get this error:

The label MyLabel is missing.

What's wrong?

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

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

发布评论

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

评论(2

债姬 2025-01-02 02:13:38

您只能中断到文本封闭语句上的标签。在您的示例中,标签不在包含 break 语句的语句上。

(无论如何,即使支持 goto 的高级语言也不允许您从外部跳转到循环的中间。我正在考虑 C ......以及更早的语言像 FORTRAN 这样的语言。)

Java 允许你越狱,但闯入是非法的……并且会让你被捕 :-)

(抱歉,我无法抗拒。对于那些不明白这个笑话的人,“闯入”是指有人进入你的房子偷东西 -请参阅“中断并输入” ...)

You can only break to a label that is on a textually enclosing statement. In your example, the label is NOT on a statement that encloses the break statement.

(For what it is worth, even higher-level languages that support goto don't allow you to jump into the middle of a loop from the outside. I'm thinking of C ... and older languages like FORTRAN.)

Java allows you to break out, but breaking in is illegal ... and will get you arrested :-)

(Sorry, I couldn't resist it. For those who don't get the joke, a "break in" is when someone enters your house to steal stuff - see "break and enter" ... )

谁对谁错谁最难过 2025-01-02 02:13:38

不能以这种常规方式使用标签作为 goto。
如果您想转到更高级别的嵌套代码,请使用标签。

例如

l1: while
 l2: while
  l3: while
   break l1
   l4: while

You can't use labels as goto in that general way.
Use labels if you want to goto a higher level of nested code.

for example

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