• 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

help for loop variation

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all.
i have a doubt
for(int i=0,System.out.println("sometext");i<6;i++){---for body--}
gives the compile time error, but
int i;
for(i=0,System.out.println("sometext");i<6;i++){--for body--}
works fine.. why?.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this piece of code



works fine, even though it does not say int j.

This is because the kind compiler uses the int declaration at the beginning of every initialization within the for statement.

Since System.out.println cannot be assigned as an int, the compiler complains.
 
vignesh Ramanathan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you sir. i understood it clearly.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastian Janisch wrote:Look at this piece of code



works fine, even though it does not say int j.

This is because the kind compiler uses the int declaration at the beginning of every initialization within the for statement.

Since System.out.println cannot be assigned as an int, the compiler complains.




Hi
Just seen your post and read something relevant. In KB SCJP6 p346, it says you can have multiple declarations in a for loop,
but they must all be the same variable TYPE. This explains why putting INT only once worked for j also.

The string output works ok in this example also in the book:



However this introduces a NEW QUESTION:
On rules roundup there is a question that you cannot declare one variable in the loop and one above it, either do both above it, or both within it - you can't mix it up. In this example it is mixed. Has this rule changed - or maybe I did not understand it corrrectly in the roundup?

Thanks

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

Nigel Shrin wrote:

In this example it is mixed.



It is not a mixed declaration. b!=1 is not in the declaration part of the for loop.
Following would be a mixed declaration and give compiler error because you are declaring b again in for loop.
 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harsh, thanks for explaining that
Nigel
 
reply
    Bookmark Topic Watch Topic
  • New Topic