views:

71

answers:

2

hi,

why i am getting undefined label error in following code?? i am ignoring code as it is of no use...

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

 if(condition)
 {
     if(condition)
     { some code     }
     else 
     { 
           some code;
           continue loopLabel;
     }
 }
+4  A: 

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)

Michael Mrozek
then... is there any alternative where i can get exact functionality as that of goto??
rohit
fortunately, you can't
Bozho
Actually, you could wrap your code in a `do { ... } while (false);` to be able to jump back to the beginning. but that would lead to unclear / unmaintainable code, so it is frowned upon.
rsp
`continue` wouldn't jump back to the beginning in that case, it would test the condition (`false`) and end the loop. You would have to do `while(true) {}` and break out at the end. But yes, it would be horrible
Michael Mrozek
i should probably restructure the program..!Thakns everyone!
rohit
+1  A: 

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

Labels are used only to break and continue loops.

Bozho
Did I say something wrong?
Bozho
No idea, but I canceled it out
Michael Mrozek
nope!! u did't say anything wrong..!Thanks for reply!
rohit