Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Unreachable label

 
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this code compile even though the start: label is unreachable. I say that it is unreachable because you will never be able to write break start or continue start

 
Sheriff
Posts: 4885
317
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The line with the 'start' label is reachable. The console output prints "start" proving the case.

It is not compulsory to reference labels:
 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind. After doing some googling, I found out that any Java statement (except a declaration) can be labelled. I guess it provides an alternative way to comment your code. However, the most common use of labels is their use in break and continue statements.

Tim Cooke wrote:The line with the 'start' label is reachable.


I meant that the start: label can never be reached by a break or continue statement.
 
Bartender
Posts: 7300
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Cox wrote:Never mind. After doing some googling, I found out that any Java statement (except a declaration) can be labelled. I guess it provides an alternative way to comment your code. However, the most common use of labels is their use in break and continue statements.


Do not use labels to comment your code, the comment syntax (//) (/**/) is much better for doing that.

Yes, labels can be combined with break and continue statements, but only use them when trying to break out of nested loops and switches. Some would argue that even using labels in this way, while syntactically correct, is equivalent to the frowned upon GOTO, and that alternatives should be used instead.
 
We find this kind of rampant individuality very disturbing. But not this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic