• 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

Labeled statements

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

why this code doesn't compile



this will compile



also this will compile



The difference I can see is using blocks { } and for block.
Are there any rules regarding labeling statements?

Thanks in advance.
 
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the name implies, a labeled statement is supposed to define a set of statements, which can be referred using a label. A set of statements can be defined as a block by either grouping all the statements using {} or by using any of the loops - while/ do-while/ for. When you issue the command break label or continue label, JVM should know the block of statements it is supposed to exit/ start over again.

cheers
Mala
 
Fahad Muhammad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mala.

I also noticed that declaring variables inside a labeled statement causes a compilation error.
So, Is it safe to say that declaring variable is NOT a statement?
 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fahad Muhammad wrote:
I also noticed that declaring variables inside a labeled statement causes a compilation error.
So, Is it safe to say that declaring variable is NOT a statement?



Fahad,

I modified your code and added a line of variable declaration within the labeled statement and it compiles fine:



Or did you mean something else?

cheers
Mala
 
Fahad Muhammad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean this which will not compile


Versus this wich will compile

 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fahad,

Labeled statements do not work with declarations. The following is 'labeled' declaration, which won't work:


Labels can be used with a looping statements (for, while, do), if constructs (if statement), expression, assignment, return, try, throws, and with a block. It is interesting to note that the above declaration can be defined within a block statement as follows:


A little, funny & weird story may help you to remember this Java Rule:

Boss (Java) was announcing bonus (privilege of using labels) for all its employees (language constructs) that were present in the office. 'declaration' wasn't awarded this privilege because it was away, partying, and hence missed on it.

I know its very weird and funny, but it will help you to retain this info!

cheers
Mala
 
Mala Gupta
Author
Posts: 375
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A little funny image may help as well:



cheers
Mala
 
Fahad Muhammad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Mala
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mala

can you please tell me why the class does not comile ,if assignment is allowed


public class Breaker {
static String o = "";
public static void main(String[] args) {
z:
o = o + 2;
for(int x = 3; x < 8; x++) {
if(x==4) break;
if(x==6) break z;
o = o + x;
}
}
}
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rohit sahai wrote:Hi Mala

can you please tell me why the class does not comile ,if assignment is allowed


public class Breaker {
static String o = "";
public static void main(String[] args) {
z:
o = o + 2;
for(int x = 3; x < 8; x++) {
if(x==4) break;
if(x==6) break z;
o = o + x;
}
}
}


Rohit, the problem is not with the assignment statement. The label 'z' is applicable only for the statement "o=o+2;"

is same as

The "break z;" statement is within the for loop which is an unlabeled block.
For labeled break statements to work, the "break label" should appear within a block labeled with the particular label.
The code should be rewritten as follows:
 
Get off me! Here, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic