aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on labeled continue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on labeled continue" Watch "Question on labeled continue" New topic
Author

Question on labeled continue

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
class Circus
{

public static void main (String []s)
{

int x=9;
int y=6;
for(int z=0; z<6; z++,y--)
{
if(x>2) x--;
label:
if(x>5){
System.out.println(x)
--x;
continue label;
}
x--
}
}
}

answer - compilation fails bcoz label is used on if staement not on loop. please explain

Source :kb


Thanks<br />Dinesh
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Dinesh,

you can only use a labeled continue with loops. If you have nested loops and want to skip an iteration of an outer loop from within an inner loop you can't simply use continue. That would only skip an iteration of the inner loop itself.
Therefore you can mark the outer loop with a label and this way skip an iteration of the outer loop by using a labeled continue in the inner loop.

Marco
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Can you please correct my code and let be know how should it be
please...
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Here is the code that compiles. You have to put the label before the (for) loop to make the program syntactically correct. Then the code will compile but of course that doesn't necessarily mean that it works semantically as intended by the author who used the label in a wrong way

[ May 10, 2008: Message edited by: Marco Ehrentreich ]
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Thanks for correcting me
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on labeled continue