中断和标签,“标签 MyLabel 丢失”
我有这样的代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只能中断到文本封闭语句上的标签。在您的示例中,标签不在包含
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" ... )
您不能以这种常规方式使用标签作为 goto。
如果您想转到更高级别的嵌套代码,请使用标签。
例如
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