• 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

A bit of confusion about "variable declared (not just initialized)", p. 362 (K&B7)

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

I find this line in the third bullet of the two-minute drill on page 362 confusing:

"A variable declared (not just initialized) within the basic for loop ..."

At first I thought it was a typo and the writer meant "A variable initialized, not just declared ... " because a variable must be declared before it is initialized, but then I realized they probably meant the following:

"A declared variable, or a declared variable that is also initialized ... "

or maybe, "A declared variable, whether or not initialized ..."

Not sure if I was the only one thrown off.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Kevin wrote:At first I thought it was a typo and the writer meant "A variable initialized, not just declared ... " because a variable must be declared before it is initialized, but then I realized they probably meant the following:

"A declared variable, or a declared variable that is also initialized ... "

or maybe, "A declared variable, whether or not initialized ..."

Not sure if I was the only one thrown off.


That statement is about nothing more than the scope of a variable which is declared in a basic for loop. So if you declare a variable in the basic for loop, you can't use it outside this for loop as illustrated in this code snippetIf you want to use the variable inside the for loop, you must initialize this variable (as you already mentioned). But you can perfectly declare a few variables without assigning a value. This code snippet compiles and runs successfully

But you could have also declared a variable before the basic for loop and initialize it inside the basic for loop. Such a variable can still be used outside the basic for loop. That's what's the "(not just initialized)" part in that statement is all about. This code snippet will compile and run successfully

Hope it helps!
Kind regards,
Roel
 
This one time, at bandcamp, I had relations with a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic