• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Labeeled Problem

 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Why this code is not compiling? and stating int j = 0 is not a staement
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
I am also surprized why it is giving compilation error.I have tried to find something reg this in JLS and the most I finds is that you can label any statement.
Can anyone ,Please help?
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A labelled statement should be followed by a while loop or a for loop. Applying a label to any other statement will produce a compiler error.
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
You can never declare a variable in a lebeled block.whereas you can assign it a value if you previously declared it.

I hope this will help
Regards Maneesh Saxena
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jothi Shankar Kumar Sankararaj:
A labelled statement should be followed by a while loop or a for loop. Applying a label to any other statement will produce a compiler error.


sorry my dear you are bit confused,as i said previously You can never declare a variable in a lebeled block.whereas you can assign it a value if you previously declared it.
class Q28fd {
public static void main(String[] args) {
int j;
int counter = 0;
l1:
for (int i=0; i<10; i++) {
l2:{
j = 0;//Compile Time Error
while (j++ < 10) {
if (j > i) break l2;
if (j == i) {
counter++;
continue l1;
}
}
}

}
System.out.println(counter);
}
}


I hope this will help
Regards Maneesh Saxena
 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please ignore written "Compile Time Error" because it will compile & runs fine .this is to clear your confsion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic