• 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

Label in java

 
Ranch Hand
Posts: 53
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

i also understand label as goto. however we do not use goto in java , it is reserved word.
But what is purpose of putting label in simple statement ?




And below code will not compile.




error: undefined label: l2
                               break l2;


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

prateek shaw wrote:
But what is purpose of putting label in simple statement ?



Besides being an interesting quirk in the language, I don't know of any usage. If I have to hazard a guess, I would say that the implementation of the label was done that way, just in case, in the future, the goto instruction gets implemented into the language (although, that is very unlikely now).

Henry
 
Ranch Hand
Posts: 133
12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

First, the reason your code example won't compile is because you are thinking of the "break" statement as if it were a thinly disguised "goto".    

Loosely speaking, to use a label in a break statement, the label must be associated with a loop-control statement. Your label "l2" is not tied to your for statement, so it is not a valid break location.  Again, a break-with-label statement is a loop-control construct, and not the same thing as a classic goto.

The break-with-label syntax is intended to handle cases where you need to implement multiply nested loops.  If a significant condition were to occur in the inner most loop, and you needed to break out of the outter loop, you could accomplish it as shown in the code below.




Some authors argue that the break-with-label syntax should be avoided if possible (some would even piously argue that it shouldn't be part of the language).   I think that they have a point.  The syntax can make your code hard to follow, especially if your looping code gets too complex.  Often, it can be better managed by handling the inner loop in a separate method... Better yet, sometimes you can construct your logic so that a condition where the break would be necessary never happens.  However, sometimes there are cases where the break-to-label syntax is exactly what you need.  When that happens, there's no reason to not use it.  Over the last decade or so, I have probably used it a couple dozen times... mostly in image-processing, data-compression, and scientific analysis applications where I have tight loops and complex data relationships. In such cases, it can even make the code easier to read, not harder, because it keeps important logic blocks close together in the code.

Hope this helps.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic