• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on labeled continue

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please correct my code and let be know how should it be
please...
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for correcting me
 
reply
    Bookmark Topic Watch Topic
  • New Topic