This week's book giveaway is in the Android forum.
We're giving away four copies of Learning Java by Building Android Games and have Jean-Marcel Belmont on-line!
See this thread for details.
Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Continue Label Question  RSS feed

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Folks,

If I run the following class I got the � Undefined Label: One. Yes it�s correct the label one is not associated with any loop. But the whizlab says it will gives result how it�s possible. Pals explain to me.

Note Question Ref :-
Whizlabs SCJP 1.4 Exam Simulator (TestName: CUSTOMIZED EXAM6/22/2006, Q No. 14, Q Id. 218)

Thanks, Raghu.K

class Test{
public static void main(String[] args){
Test a = new Test();
a.testing();
}
void testing()
{
one:
two:
for(int i = 0; i<3; i++)
{
three:
for(int j = 0; j<3; j +=10)
{
System.out.println(i+j);
if(i >2)
continue one;
}
}
}

}
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ragu:
There is a rule for labeled continue and break statement that should be followed otherwise u will get compile time error. And the rule is :

"Labeled continue and break statements must be inside the loop that
has the same label name otherwise, the code will not compile." from K&B book

Convert ur code as follows...and it will compile & run fine:

class Test{
public static void main(String[] args){
Test a = new Test();
a.testing();
}
void testing()
{
one:
for(int k=0;k<3;k++){
two:
for(int i = 0; i<3; i++)
{
three:
for(int j = 0; j<3; j +=10)
{
System.out.println(i+j);
if(i >2)
continue one;
}
}
}
}
}

Hope it helps...


Regards
Swapnil
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swapnil according to K&B book in this code the loop is assoisated first with label "two" right.so in continue statement if we put
"continue two" it works fine.is this correct.please correct me if iam wrong .

 
Swapnil Trivedi
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes chitti, "continue two" will work absolutely fine.
In the case of "continue two" the control will go to the next iteration of for(i=0;i<3;i++) loop. Hence it will print 0,1,2.



Regards
Swapnil
[ June 22, 2006: Message edited by: Swapnil Trivedi ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!