• 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

Can label be placed before switch?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K&B book:
"it's most common to use labels with loop statements like for or while,in conjunction with break and continue statements.A label statement must be placed just before the statement being labeled,and it consists of a valid identifier that ends with a colon( "



This code compiles and runs well.Can label be placed before switch?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A label is not executable code, so you can put labels in all sorts of places.

Why you should want a label, now that's a different matter, unless it's "I just wanted to see what would happen," which is an adequate justification for anything, never mind how daft.
 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
A label is not executable code, so you can put labels in all sorts of places.

Why you should want a label, now that's a different matter, unless it's "I just wanted to see what would happen," which is an adequate justification for anything, never mind how daft.



I don't think so,label can't be put any sorts of places:

it compiles error.label can only be put before for or while.
This is a SCJP question,so I'm confused about that.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can put label before any statement or block of code.
but you cannot call break labelX if you are not in its scope.
you are trying to call break label1 in switch case 3 but it is out of scope.
look this code about scopes:
 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ivan Ivanic:
you can put label before any statement or block of code.
but you cannot call break labelX if you are not in its scope.
you are trying to call break label1 in switch case 3 but it is out of scope.
look this code about scopes:



Ivan: is this means that if i put label just before for or while like

this label1 will implicitly add block?
 
Ivan Ivanic
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I guess you could say it like that. but that is the point.
 
Gary Kevin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thinks all!
 
reply
    Bookmark Topic Watch Topic
  • New Topic