• Post Reply Bookmark Topic Watch Topic
  • New Topic

Continue Label Question  RSS feed

 
RAGU KANNAN
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;
}
}
}

}
 
Swapnil Trivedi
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
 
sri latha
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!