开发者

Why am I getting an "undefined label" error in java?

开发者 https://www.devze.com 2022-12-28 11:05 出处:网络
Why am I getting an undefined label error in following code? (I am leaving out some irrelevant code ...)

Why am I getting an undefined label error in following code? (I am leaving out some irrelevant code ...)

 loopLabel: 
 for(i=0;;i++)
 {
   { // some code;
   }
   { // some code;
   }
 }

 if(condition开发者_开发知识库)
 {
     if(condition)
     { // some code
     }
     else 
     { 
           //some code;
           continue loopLabel;
     }
 }


  


continue is used to skip to the start of a new iteration of a loop; you use a label if you have nested loops and you want to specify which one to jump to. You're trying to use it like a goto to jump to a totally unrelated section of code, which isn't allowed

Legal usage is something like:

foo:
while(cond1) {
    code;
    while(cond2) {
        if(cond3) {
            continue foo;
        }
    }
}

(Java guide on branching statements)


Because you are outside of the loop. The label is visible only inside the loop.

Labels are used only to break and continue loops.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号