File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on labeled continue

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please correct my code and let be know how should it be
please...
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic