“未定义标签"使用带标签的break时java中的错误 [英] "Undefined label" error in java when using labeled break

查看:158
本文介绍了“未定义标签"使用带标签的break时java中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm trying to analyse a java code and see what it does, but in the "break label" line it gives the "Undefined label" error. from my understanding java unlike c# allows breaking into a scope outside of the current scope. isn't that right?

if (conditions) {
  // some code
  if (conditions) {
    break label;
  }
  // some code
}

for (;;) {
  if (conditions) {
    // some code
  }
  // some code
  break;
  label: 
  // some code
}

解决方案

In your example, you are trying to use break label; as though it were similar to the goto label; syntax in C/C++. This is not how it works in Java.

in Java, the break label; syntax is applicable only when you are using nested loops and you need to break out from an inner loop to one of the outer loops in question.

For example:

outer: while(some condition) {
    inner: while(another condition) {
        innermost: while(yet another condition) {
            if(breaking_condition) {
                break inner;
            } else if (one more breaking condition) {
                break outer;
            }
        }
    }
}

Refer this tutorial from Oracle:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html


Hope this helps!

这篇关于“未定义标签"使用带标签的break时java中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆