• 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

Arbitrary code blocks

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried the following - just out of curiosity;


I was slightly surprised when it compiled and produced


The only use I could think of was limiting variable scope - but that seems redundant given the limiting provided within 'normal' code blocks (if, for, etc)

Is this just 'the way it works', or is there a practical use for declaring arbitrary code blocks?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The extra brackets are valid syntax, but meaningless in the case you showed. The language rule is probably: Any place you could have a single statement you could also have open brace + many statements + close brace. This makes great sense in the code following if or while. It also just happens to work in the middle of sequential statements. I see extra brackets in real code sometimes after (someone else's) lazy editing, like removing a try-catch or something.

Just for grins, follow up on the variable scoping question and try some variations.

Are both "int i" declarations allowed? If so, can you think of any reason we'd do that on purpose?
 
Simon Birch
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes - I tried that sort of thing.

Again - the only reason I can think of to do this would be to limit the scope and life of a variable. I guess this is useful to clarify the intent of code, but I thought that methods ought to be kept as short as possible anyway. Adding an arbitrary block of code still seems redundant.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, I agree.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's mostly there for language consistency, I guess.
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as 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